-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from joshtriplett/edition-guide-rustfmt-singl…
…e-line-where-clauses 2024: Add chapter on single-line `where` clauses
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Rustfmt: Single-line where clauses | ||
|
||
## Summary | ||
|
||
When an associated type or method declaration has a single bound in the `where` clause, it can now sometimes be formatted on one line. | ||
|
||
## Details | ||
|
||
In general, the [Rust Style Guide](../../style-guide/index.html) states to wrap `where` clauses onto subsequent lines, with the keyword `where` on a line of its own and then the individual clauses indented on subsequent lines. | ||
|
||
However, in the 2024 Edition, when writing an associated type declaration or a method declaration (with no body), a short `where` clause can appear on the same line. | ||
|
||
This is particularly useful for generic associated types (GATs), which often need `Self: Sized` bounds. | ||
|
||
In Rust 2021 and before, this would look like: | ||
|
||
```rust | ||
trait MyTrait { | ||
fn new(&self) -> Self | ||
where | ||
Self: Sized; | ||
|
||
type Item<'a>: Send | ||
where | ||
Self: 'a; | ||
} | ||
``` | ||
|
||
In the 2024 Edition, `rustfmt` now produces: | ||
|
||
```rust | ||
trait MyTrait { | ||
fn new(&self) -> Self where Self: Sized; | ||
|
||
type Item<'a>: Send where Self: 'a; | ||
} | ||
``` | ||
|
||
## 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 |