@@ -2285,22 +2285,23 @@ impl<T> [T] {
2285
2285
///
2286
2286
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
2287
2287
/// 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
2289
2291
#[ unstable( feature = "is_sorted" , reason = "new API" , issue = "53485" ) ]
2290
2292
pub fn is_sorted_by < F > ( & self , mut compare : F ) -> bool
2291
2293
where
2292
2294
F : FnMut ( & T , & T ) -> Option < Ordering >
2293
2295
{
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
+ }
2298
2300
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 ) {
2301
2303
return false ;
2302
2304
}
2303
- last = & curr;
2304
2305
}
2305
2306
2306
2307
true
@@ -2309,9 +2310,11 @@ impl<T> [T] {
2309
2310
/// Checks if the elements of this slice are sorted using the given key extraction function.
2310
2311
///
2311
2312
/// 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
2313
2314
/// documentation for more information.
2314
2315
///
2316
+ /// [`is_sorted`]: #method.is_sorted
2317
+ ///
2315
2318
/// # Examples
2316
2319
///
2317
2320
/// ```
0 commit comments