@@ -2293,22 +2293,23 @@ impl<T> [T] {
22932293 ///
22942294 /// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
22952295 /// function to determine the ordering of two elements. Apart from that, it's equivalent to
2296- /// `is_sorted`; see its documentation for more information.
2296+ /// [`is_sorted`]; see its documentation for more information.
2297+ ///
2298+ /// [`is_sorted`]: #method.is_sorted
22972299 #[ unstable( feature = "is_sorted" , reason = "new API" , issue = "53485" ) ]
22982300 pub fn is_sorted_by < F > ( & self , mut compare : F ) -> bool
22992301 where
23002302 F : FnMut ( & T , & T ) -> Option < Ordering >
23012303 {
2302- let mut last = match self . first ( ) {
2303- Some ( e ) => e ,
2304- None => return true ,
2305- } ;
2304+ let len = self . len ( ) ;
2305+ if len <= 1 {
2306+ return true ;
2307+ }
23062308
2307- for curr in & self [ 1 ..] {
2308- if compare ( & last , & curr ) . map ( |o| o == Ordering :: Greater ) . unwrap_or ( true ) {
2309+ for i in 1 ..len {
2310+ if compare ( & self [ i - 1 ] , & self [ i ] ) . map ( |o| o == Ordering :: Greater ) . unwrap_or ( true ) {
23092311 return false ;
23102312 }
2311- last = & curr;
23122313 }
23132314
23142315 true
@@ -2317,9 +2318,11 @@ impl<T> [T] {
23172318 /// Checks if the elements of this slice are sorted using the given key extraction function.
23182319 ///
23192320 /// Instead of comparing the slice's elements directly, this function compares the keys of the
2320- /// elements, as determined by `f`. Apart from that, it's equivalent to `is_sorted`; see its
2321+ /// elements, as determined by `f`. Apart from that, it's equivalent to [ `is_sorted`] ; see its
23212322 /// documentation for more information.
23222323 ///
2324+ /// [`is_sorted`]: #method.is_sorted
2325+ ///
23232326 /// # Examples
23242327 ///
23252328 /// ```
0 commit comments