Skip to content

Commit

Permalink
Merge pull request #332 from calebcartwright/rustfmt-sorting
Browse files Browse the repository at this point in the history
2024: rustfmt sorting
  • Loading branch information
ehuss authored Nov 18, 2024
2 parents 915f9b3 + 7826b2c commit 07766cb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
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

0 comments on commit 07766cb

Please sign in to comment.