|
| 1 | +# Rustfmt: Style Edition |
| 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 issue at <https://github.com/rust-lang/rust/issues/123799>. |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +User can now control which style edition to use with `rustfmt`. |
| 10 | + |
| 11 | +## Details |
| 12 | + |
| 13 | +The default formatting produced by Rustfmt is governed |
| 14 | +by the rules in the [Rust Style Guide]. |
| 15 | + |
| 16 | +Additionally, Rustfmt has a formatting stability guarantee that aims |
| 17 | +to avoid causing noisy formatting churn for users when updating a |
| 18 | +Rust toolchain. This stability guarantee essentially means that a newer |
| 19 | +version of Rustfmt cannot modify the _successfully formatted_ output |
| 20 | +that was produced by a previous version of Rustfmt. |
| 21 | + |
| 22 | +The combination of those two constraints had historically locked both |
| 23 | +the Style Guide and the default formatting behavior in Rustfmt. This |
| 24 | +impasse caused various challenges, such as preventing the ability to |
| 25 | +iterate on style improvements, and requiring Rustfmt to maintain legacy |
| 26 | +formatting quirks that were obviated long ago (e.g. nested tuple access). |
| 27 | + |
| 28 | +[RFC 3338] resolved this impasse by establishing a mechanism for the |
| 29 | +Rust Style Guide to be aligned to Rust's Edition model wherein the |
| 30 | +Style Guide could evolve across Editions, and `rustfmt` would allow users |
| 31 | +to specify their desired Edition of the Style Guide, referred to as the Style Edition. |
| 32 | + |
| 33 | +In the 2024 Edition, `rustfmt` now supports the ability for users to control |
| 34 | +the Style Edition used for formatting. The 2024 Edition of the Style Guide also |
| 35 | +includes enhancements to the Style Guide which are detailed elsewhere in this Edition Guide. |
| 36 | + |
| 37 | +By default `rustfmt` will use the same Style Edition as the standard Rust Edition |
| 38 | +used for parsing, but the Style Edition can also be overridden and configured separately. |
| 39 | + |
| 40 | +There are multiple ways to run `rustfmt` with the 2024 Style Edition: |
| 41 | + |
| 42 | +With a `Cargo.toml` file that has `edition` set to `2024`, run: |
| 43 | + |
| 44 | +```sh |
| 45 | +cargo fmt |
| 46 | +``` |
| 47 | + |
| 48 | +Or run `rustfmt` directly with `2024` for the edition to use the 2024 edition |
| 49 | +for both parsing and the 2024 edition of the Style Guide: |
| 50 | + |
| 51 | +```sh |
| 52 | +rustfmt lib.rs --edition 2024 |
| 53 | +``` |
| 54 | + |
| 55 | +The style edition can also be set in a `rustfmt.toml` configuration file: |
| 56 | +```toml |
| 57 | +style_edition = "2024" |
| 58 | +``` |
| 59 | + |
| 60 | +Which is then used when running `rustfmt` directly: |
| 61 | +```sh |
| 62 | +rustfmt lib.rs |
| 63 | +``` |
| 64 | + |
| 65 | +Alternatively, the style edition can be specified directly from `rustfmt` options: |
| 66 | + |
| 67 | +```sh |
| 68 | +rustfmt lib.rs --style-edition 2024 |
| 69 | +``` |
| 70 | + |
| 71 | +[Rust Style Guide]: ../../style-guide/index.html |
| 72 | +[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html |
| 73 | + |
| 74 | +## Migration |
| 75 | + |
| 76 | +Running `cargo fmt` or `rustfmt` with the 2024 edition or style edition will |
| 77 | +automatically migrate formatting over to the 2024 style edition formatting. |
| 78 | + |
| 79 | +Projects who have contributors that may utilize their editor's format-on-save |
| 80 | +features are also strongly encouraged to add a `.rustfmt.toml` file to their project |
| 81 | +that includes the corresponding `style_edition` utilized within their project, or to |
| 82 | +encourage their users to ensure their local editor format-on-save feature is |
| 83 | +configured to use that same `style_edition`. |
| 84 | + |
| 85 | +This is to ensure that the editor format-on-save output is consistent with the |
| 86 | +output when `cargo fmt` is manually executed by the developer, or the project's CI |
| 87 | +process (many editors will run `rustfmt` directly which by default uses the 2015 |
| 88 | +edition, whereas `cargo fmt` uses the edition specified in the `Cargo.toml` file) |
0 commit comments