Skip to content

Commit dc8d633

Browse files
committed
Improve documentation and slice impl
1 parent 6445dea commit dc8d633

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/libcore/iter/iterator.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,9 @@ pub trait Iterator {
26202620
///
26212621
/// Instead of using `PartialOrd::partial_cmp`, this function uses the given `compare`
26222622
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
2623-
/// `is_sorted`; see its documentation for more information.
2623+
/// [`is_sorted`]; see its documentation for more information.
2624+
///
2625+
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
26242626
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
26252627
fn is_sorted_by<F>(mut self, mut compare: F) -> bool
26262628
where
@@ -2646,9 +2648,11 @@ pub trait Iterator {
26462648
/// function.
26472649
///
26482650
/// Instead of comparing the iterator's elements directly, this function compares the keys of
2649-
/// the elements, as determined by `f`. Apart from that, it's equivalent to `is_sorted`; see
2651+
/// the elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see
26502652
/// its documentation for more information.
26512653
///
2654+
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
2655+
///
26522656
/// # Examples
26532657
///
26542658
/// ```

src/libcore/slice/mod.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)