Skip to content

Commit a24c81d

Browse files
authored
Rollup merge of rust-lang#40619 - stjepang:unstable-book-sort-unstable, r=frewsxcv
Add docs for sort_unstable to unstable book Tracking issue for the feature: rust-lang#40585 r? @steveklabnik
2 parents c5f3bf1 + 6acbbc6 commit a24c81d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/doc/unstable-book/src/sort-unstable.md

+31
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,35 @@ The tracking issue for this feature is: [#40585]
66

77
------------------------
88

9+
The default `sort` method on slices is stable. In other words, it guarantees
10+
that the original order of equal elements is preserved after sorting. The
11+
method has several undesirable characteristics:
912

13+
1. It allocates a sizable chunk of memory.
14+
2. If you don't need stability, it is not as performant as it could be.
15+
16+
An alternative is the new `sort_unstable` feature, which includes these
17+
methods for sorting slices:
18+
19+
1. `sort_unstable`
20+
2. `sort_unstable_by`
21+
3. `sort_unstable_by_key`
22+
23+
Unstable sorting is generally faster and makes no allocations. The majority
24+
of real-world sorting needs doesn't require stability, so these methods can
25+
very often come in handy.
26+
27+
Another important difference is that `sort` lives in `libstd` and
28+
`sort_unstable` lives in `libcore`. The reason is that the former makes
29+
allocations and the latter doesn't.
30+
31+
A simple example:
32+
33+
```rust
34+
#![feature(sort_unstable)]
35+
36+
let mut v = [-5, 4, 1, -3, 2];
37+
38+
v.sort_unstable();
39+
assert!(v == [-5, -3, 1, 2, 4]);
40+
```

0 commit comments

Comments
 (0)