@@ -2285,22 +2285,23 @@ impl<T> [T] {
22852285 ///
22862286 /// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
22872287 /// function to determine the ordering of two elements. Apart from that, it's equivalent to
2288- /// `is_sorted`; see its documentation for more information.
2288+ /// [`is_sorted`]; see its documentation for more information.
2289+ ///
2290+ /// [`is_sorted`]: #method.is_sorted
22892291 #[ unstable( feature = "is_sorted" , reason = "new API" , issue = "53485" ) ]
22902292 pub fn is_sorted_by < F > ( & self , mut compare : F ) -> bool
22912293 where
22922294 F : FnMut ( & T , & T ) -> Option < Ordering >
22932295 {
2294- let mut last = match self . first ( ) {
2295- Some ( e ) => e ,
2296- None => return true ,
2297- } ;
2296+ let len = self . len ( ) ;
2297+ if len <= 1 {
2298+ return true ;
2299+ }
22982300
2299- for curr in & self [ 1 ..] {
2300- if compare ( & last , & curr ) . map ( |o| o == Ordering :: Greater ) . unwrap_or ( true ) {
2301+ for i in 1 ..len {
2302+ if compare ( & self [ i - 1 ] , & self [ i ] ) . map ( |o| o == Ordering :: Greater ) . unwrap_or ( true ) {
23012303 return false ;
23022304 }
2303- last = & curr;
23042305 }
23052306
23062307 true
@@ -2309,9 +2310,11 @@ impl<T> [T] {
23092310 /// Checks if the elements of this slice are sorted using the given key extraction function.
23102311 ///
23112312 /// Instead of comparing the slice's elements directly, this function compares the keys of the
2312- /// elements, as determined by `f`. Apart from that, it's equivalent to `is_sorted`; see its
2313+ /// elements, as determined by `f`. Apart from that, it's equivalent to [ `is_sorted`] ; see its
23132314 /// documentation for more information.
23142315 ///
2316+ /// [`is_sorted`]: #method.is_sorted
2317+ ///
23152318 /// # Examples
23162319 ///
23172320 /// ```
0 commit comments