Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024: rustfmt sorting #332

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
- [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md)
- [Cargo: Reject unused inherited default-features](rust-2024/cargo-inherited-default-features.md)
- [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md)
- [Rustfmt: Style edition](rust-2024/rustfmt-style-edition.md)
- [Rustfmt: Raw identifier sorting](rust-2024/rustfmt-raw-identifier-sorting.md)
- [Rustfmt: Style Edition](rust-2024/rustfmt-style-edition.md)
- [Rustfmt: Version sorting](rust-2024/rustfmt-version-sorting.md)
- [`gen` keyword](rust-2024/gen-keyword.md)
- [Macro fragment specifiers](rust-2024/macro-fragment-specifiers.md)
- [Missing macro fragment specifiers](rust-2024/missing-macro-fragment-specifiers.md)
Expand Down
14 changes: 2 additions & 12 deletions src/rust-2024/rustfmt-raw-identifier-sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ use websocket::result::WebSocketError;

## Migration

The change can be applied automatically by running `cargo fmt` or `rustfmt` with the 2024 Edition.
The change can be applied automatically by running `cargo fmt` or `rustfmt` with the 2024 Edition. See the [Style edition] chapter for more information on migrating and how style editions work.

With a `Cargo.toml` file that has `edition` set to `2024`, run:

```sh
cargo fmt
```

Or run `rustfmt` directly:

```sh
rustfmt foo.rs --style-edition 2024
```
[Style edition]: rustfmt-style-edition.md
2 changes: 1 addition & 1 deletion src/rust-2024/rustfmt-style-edition.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Rustfmt: Style Edition
# Rustfmt: Style edition

🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".

Expand Down
45 changes: 45 additions & 0 deletions src/rust-2024/rustfmt-version-sorting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Rustfmt: Version sorting

🚧 The 2024 Edition has not yet been released and hence this section is still "under construction".

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

## Summary

`rustfmt` utilizes a new sorting algorithm.

## Details

The [Rust Style Guide] includes [rules for sorting][sorting] that `rustfmt` applies in various contexts, such as on imports.

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.

For example with a given (unsorted) input:

```rust,ignore
use std::num::{NonZeroU32, NonZeroU16, NonZeroU8, NonZeroU64};
use std::io::{Write, Read, stdout, self};
```

In the prior Editions, `rustfmt` would have produced:

```rust,ignore
use std::io::{self, stdout, Read, Write};
use std::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8};
```

In the 2024 Edition, `rustfmt` now produces:

```rust,ignore
use std::io::{self, Read, Write, stdout};
use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64};
```

[Rust Style Guide]: ../../style-guide/index.html
[sorting]: ../../style-guide/index.html#sorting

## Migration

The change can be applied automatically by running `cargo fmt` or `rustfmt` with the 2024 Edition. See the [Style edition] chapter for more information on migrating and how style editions work.

[Style edition]: rustfmt-style-edition.md