]> git.lizzy.rs Git - rust.git/blob - src/docs/stable_sort_primitive.txt
Rollup merge of #104595 - compiler-errors:poly-existential-predicate, r=lcnr
[rust.git] / src / docs / stable_sort_primitive.txt
1 ### What it does
2 When sorting primitive values (integers, bools, chars, as well
3 as arrays, slices, and tuples of such items), it is typically better to
4 use an unstable sort than a stable sort.
5
6 ### Why is this bad?
7 Typically, using a stable sort consumes more memory and cpu cycles.
8 Because values which compare equal are identical, preserving their
9 relative order (the guarantee that a stable sort provides) means
10 nothing, while the extra costs still apply.
11
12 ### Known problems
13
14 As pointed out in
15 [issue #8241](https://github.com/rust-lang/rust-clippy/issues/8241),
16 a stable sort can instead be significantly faster for certain scenarios
17 (eg. when a sorted vector is extended with new data and resorted).
18
19 For more information and benchmarking results, please refer to the
20 issue linked above.
21
22 ### Example
23 ```
24 let mut vec = vec![2, 1, 3];
25 vec.sort();
26 ```
27 Use instead:
28 ```
29 let mut vec = vec![2, 1, 3];
30 vec.sort_unstable();
31 ```