Skip to content

Commit

Permalink
docs: clarify the editions behavior and it's consequences on formatti…
Browse files Browse the repository at this point in the history
…ng (#6486)

Update `style_edition` and `edition` entries in `Configurations.md`, and encourage users to explicitly configure `style_edition` and `edition` in `rustfmt.toml` to ensure rustfmt and `cargo fmt` produce the same formatting.
  • Loading branch information
karolzwolak authored Feb 27, 2025
1 parent 96264d2 commit c6c8159
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
23 changes: 20 additions & 3 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,17 @@ Specifies which edition is used by the parser.
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"`
- **Stable**: Yes

Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if executed
through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition needs to be specified
in your config file:
The `edition` option determines the Rust language edition used for parsing the code. This is important for syntax compatibility but does not directly control formatting behavior (see [style_edition](#style_edition)).

When running `cargo fmt`, the `edition` is automatically read from the `Cargo.toml` file. However, when running `rustfmt` directly the `edition` defaults to 2015 if not explicitly configured. For consistent parsing between rustfmt and `cargo fmt` you should configure the `edition`.
For example in your `rustfmt.toml` file:

```toml
edition = "2018"
```

Alternatively, you can use the `--edition` flag when running `rustfmt` directly.

## `empty_item_single_line`

Put empty-body functions and impls on a single line
Expand Down Expand Up @@ -2803,6 +2806,20 @@ Controls the edition of the [Rust Style Guide] to use for formatting ([RFC 3338]
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"` (unstable variant)
- **Stable**: No

This option is inferred from the [`edition`](#edition) if not specified.

See [Rust Style Editions] for details on formatting differences between style editions.
rustfmt has a default style edition of `2015` while `cargo fmt` infers the style edition from the `edition` set in `Cargo.toml`. This can lead to inconsistencies between `rustfmt` and `cargo fmt` if the style edition is not explicitly configured.

To ensure consistent formatting, it is recommended to specify the `style_edition` in a `rustfmt.toml` configuration file. For example:

```toml
style_edition = "2024"
```

Alternatively, you can use the `--style-edition` flag when running `rustfmt` directly.

[Rust Style Editions]: https://doc.rust-lang.org/nightly/style-guide/editions.html?highlight=editions#rust-style-editions
[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html

Expand Down
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,36 @@ See [GitHub page](https://rust-lang.github.io/rustfmt/) for details.

### Rust's Editions

Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if
executed through the Cargo's formatting tool `cargo fmt`. Otherwise, the edition
needs to be specified in `rustfmt.toml`, e.g., with `edition = "2018"`.
The `edition` option determines the Rust language edition used for parsing the code. This is important for syntax compatibility but does not directly control formatting behavior (see [Style Editions](#style-editions)).

When running `cargo fmt`, the `edition` is automatically read from the `Cargo.toml` file. However, when running `rustfmt` directly the `edition` defaults to 2015 if not explicitly configured. For consistent parsing between rustfmt and `cargo fmt` you should configure the `edition`.
For example in your `rustfmt.toml` file:

```toml
edition = "2018"
```

### Style Editions

This option is inferred from the [`edition`](#rusts-editions) if not specified.

See [Rust Style Editions] for details on formatting differences between style editions.
rustfmt has a default style edition of `2015` while `cargo fmt` infers the style edition from the `edition` set in `Cargo.toml`. This can lead to inconsistencies between `rustfmt` and `cargo fmt` if the style edition is not explicitly configured.

To ensure consistent formatting, it is recommended to specify the `style_edition` in a `rustfmt.toml` configuration file. For example:

```toml
style_edition = "2024"
```
[Rust Style Editions]: https://doc.rust-lang.org/nightly/style-guide/editions.html?highlight=editions#rust-style-editions
[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html

## Tips

* To ensure consistent parsing between `cargo fmt` and `rustfmt`, you should configure the [`edition`](#rusts-editions) in your `rustfmt.toml` file.
* To ensure consistent formatting between `cargo fmt` and `rustfmt`, you should configure the [`style_edition`](#style-editions) in your `rustfmt.toml` file.

* For things you do not want rustfmt to mangle, use `#[rustfmt::skip]`
* To prevent rustfmt from formatting a macro or an attribute,
use `#[rustfmt::skip::macros(target_macro_name)]` or
Expand Down

0 comments on commit c6c8159

Please sign in to comment.