Skip to content

Commit ccc027e

Browse files
committed
Improve documentation and slice impl
1 parent ce47dde commit ccc027e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/libcore/iter/iterator.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,7 +2640,9 @@ pub trait Iterator {
26402640
///
26412641
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
26422642
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
2643-
/// `is_sorted`; see its documentation for more information.
2643+
/// [`is_sorted`]; see its documentation for more information.
2644+
///
2645+
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
26442646
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
26452647
fn is_sorted_by<F>(mut self, mut compare: F) -> bool
26462648
where
@@ -2666,9 +2668,11 @@ pub trait Iterator {
26662668
/// function.
26672669
///
26682670
/// Instead of comparing the iterator's elements directly, this function compares the keys of
2669-
/// the elements, as determined by `f`. Apart from that, it's equivalent to `is_sorted`; see
2671+
/// the elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see
26702672
/// its documentation for more information.
26712673
///
2674+
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
2675+
///
26722676
/// # Examples
26732677
///
26742678
/// ```

src/libcore/slice/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)