Skip to content

Commit 54c94fd

Browse files
add rustfmt sorting
1 parent 0386f15 commit 54c94fd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- [Cargo: Reject unused inherited default-features](rust-2024/cargo-inherited-default-features.md)
5050
- [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md)
5151
- [Rustfmt: Raw identifier sorting](rust-2024/rustfmt-raw-identifier-sorting.md)
52+
- [Rustfmt: sorting](rust-2024/rustfmt-sorting.md)
5253
- [`gen` keyword](rust-2024/gen-keyword.md)
5354
- [Macro fragment specifiers](rust-2024/macro-fragment-specifiers.md)
5455
- [Missing macro fragment specifiers](rust-2024/missing-macro-fragment-specifiers.md)

src/rust-2024/rustfmt-sorting.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Rustfmt: Raw identifier sorting
2+
3+
🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".
4+
5+
More information may be found in the tracking issues at <https://github.com/rust-lang/rust/issues/123800> and <https://github.com/rust-lang/rust/issues/123802>.
6+
7+
## Summary
8+
9+
`rustfmt` utilizes a new sorting algorithm.
10+
11+
## Details
12+
13+
The [Rust Style Guide] includes [rules for sorting][sorting] that `rustfmt` applies in various contexts, such as on imports.
14+
15+
Previous versions of the Style Guide and Rustfmt generally used an "ASCIIbetical" based approach. In the 2024 Edition this is changed to use a version-sort like algorithm that compares Unicode characters lexicographically and provides better results in ASCII digit comparisons.
16+
17+
For example with a given (unsorted) input:
18+
19+
```rust,ignore
20+
use std::num::{NonZeroU32, NonZeroU16, NonZeroU8, NonZeroU64};
21+
use std::io::{Write, Read, stdout, self};
22+
```
23+
24+
In the prior Editions, `rustfmt` would have produced:
25+
26+
```rust,ignore
27+
use std::io::{self, stdout, Read, Write};
28+
use std::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};
29+
```
30+
31+
In the 2024 Edition, `rustfmt` now produces:
32+
33+
```rust,ignore
34+
use std::io::{self, Read, Write, stdout};
35+
use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64};
36+
```
37+
38+
[Rust Style Guide]: ../../style-guide/index.html
39+
[sorting]: ../../style-guide/index.html#sorting
40+
41+
## Migration
42+
43+
The change can be applied automatically by running `cargo fmt` or `rustfmt` with the 2024 Edition.

0 commit comments

Comments
 (0)