Skip to content

Commit c7f855b

Browse files
joshtripletttraviscross
authored andcommitted
2024: Add chapter on single-line where clauses
1 parent 1286692 commit c7f855b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@
6767
- [Rustfmt](rust-2024/rustfmt.md)
6868
- [Rustfmt: Style edition](rust-2024/rustfmt-style-edition.md)
6969
- [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md)
70+
- [Rustfmt: Single-line `where` clauses](rust-2024/rustfmt-single-line-where-clauses.md)
7071
- [Rustfmt: Raw identifier sorting](rust-2024/rustfmt-raw-identifier-sorting.md)
7172
- [Rustfmt: Version sorting](rust-2024/rustfmt-version-sorting.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Rustfmt: Single-line where clauses
2+
3+
## Summary
4+
5+
When an associated type or method declaration has a single bound in the `where` clause, it can now sometimes be formatted on one line.
6+
7+
## Details
8+
9+
In general, the [Rust Style Guide](../../style-guide/) 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.
10+
11+
However, in the 2024 Style Edition, when writing an associated type declaration or a method declaration (with no body), a short `where` clause can appear on the same line.
12+
13+
This is particularly useful for generic associated types (GATs), which often need `Self: Sized` bounds.
14+
15+
In Rust 2021 and before, this would look like:
16+
17+
```rust
18+
trait MyTrait {
19+
fn new(&self) -> Self
20+
where
21+
Self: Sized;
22+
23+
type Item<'a>: Send
24+
where
25+
Self: 'a;
26+
}
27+
```
28+
29+
In the 2024 Edition, `rustfmt` now produces:
30+
31+
```rust
32+
trait MyTrait {
33+
fn new(&self) -> Self where Self: Sized;
34+
35+
type Item<'a>: Send where Self: 'a;
36+
}
37+
```
38+
39+
## Migration
40+
41+
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.
42+
43+
[Style edition]: rustfmt-style-edition.md

0 commit comments

Comments
 (0)