|
| 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