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