Skip to content

Commit b911dba

Browse files
committed
Slice total example: Move closer to total defn
1 parent e36bbc8 commit b911dba

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/liballoc/slice.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ impl<T> [T] {
218218
/// * total and antisymmetric: exactly one of a < b, a == b or a > b is true; and
219219
/// * transitive, a < b and b < c implies a < c. The same must hold for both == and >.
220220
///
221+
/// For example, while `f64` doesn't implement `Ord` because `NaN != NaN`, we can use
222+
/// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
223+
///
224+
/// ```
225+
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
226+
/// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
227+
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
228+
/// ```
229+
///
221230
/// When applicable, unstable sorting is preferred because it is generally faster than stable
222231
/// sorting and it doesn't allocate auxiliary memory.
223232
/// See [`sort_unstable_by`](#method.sort_unstable_by).
@@ -242,12 +251,6 @@ impl<T> [T] {
242251
/// // reverse sorting
243252
/// v.sort_by(|a, b| b.cmp(a));
244253
/// assert!(v == [5, 4, 3, 2, 1]);
245-
///
246-
/// // While f64 doesn't implement Ord because NaN != NaN, we can use
247-
/// // partial_cmp here because we know none of the elements are NaN.
248-
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
249-
/// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
250-
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
251254
/// ```
252255
#[stable(feature = "rust1", since = "1.0.0")]
253256
#[inline]

src/libcore/slice/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,15 @@ impl<T> [T] {
13461346
/// * total and antisymmetric: exactly one of a < b, a == b or a > b is true; and
13471347
/// * transitive, a < b and b < c implies a < c. The same must hold for both == and >.
13481348
///
1349+
/// For example, while `f64` doesn't implement `Ord` because `NaN != NaN`, we can use
1350+
/// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
1351+
///
1352+
/// ```
1353+
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
1354+
/// floats.sort_by(|a, b| a.partial_cmp(b).unwrap());
1355+
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
1356+
/// ```
1357+
///
13491358
/// # Current implementation
13501359
///
13511360
/// The current algorithm is based on [pattern-defeating quicksort][pdqsort] by Orson Peters,
@@ -1367,12 +1376,6 @@ impl<T> [T] {
13671376
/// // reverse sorting
13681377
/// v.sort_unstable_by(|a, b| b.cmp(a));
13691378
/// assert!(v == [5, 4, 3, 2, 1]);
1370-
///
1371-
/// // While f64 doesn't implement Ord because NaN != NaN, we can use
1372-
/// // partial_cmp here because we know none of the elements are NaN.
1373-
/// let mut floats = [5f64, 4.0, 1.0, 3.0, 2.0];
1374-
/// floats.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
1375-
/// assert_eq!(floats, [1.0, 2.0, 3.0, 4.0, 5.0]);
13761379
/// ```
13771380
///
13781381
/// [pdqsort]: https://github.com/orlp/pdqsort

0 commit comments

Comments
 (0)