Skip to content

Commit 6d3d775

Browse files
Rollup merge of #112912 - joshtriplett:style-let-else-clarifications, r=calebcartwright
style-guide: Rewrite let-else section for clarity, without changing formatting The section as written did not cover all cases, and left some of them implicit. Rewrite it to systematically cover all cases. Place examples immediately following the corresponding case. In the process, reorder to move the simplest cases first: start with single-line and add progressively more line breaks. This does not change the meaning of the section at all, and in particular does not change the defined style for let-else statements.
2 parents a1f2f23 + b551730 commit 6d3d775

File tree

1 file changed

+62
-43
lines changed

1 file changed

+62
-43
lines changed

src/doc/style-guide/src/statements.md

+62-43
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,69 @@ let Foo {
103103

104104
#### else blocks (let-else statements)
105105

106-
If a let statement contains an `else` component, also known as a let-else statement,
107-
then the `else` component should be formatted according to the same rules as the `else` block
108-
in [control flow expressions (i.e. if-else, and if-let-else expressions)](./expressions.md#control-flow-expressions).
109-
Apply the same formatting rules to the components preceding
110-
the `else` block (i.e. the `let pattern: Type = initializer_expr ...` portion)
111-
as described [above](#let-statements)
112-
113-
Similarly to if-else expressions, if the initializer
114-
expression is multi-lined, then the `else` keyword and opening brace of the block (i.e. `else {`)
115-
should be put on the same line as the end of the initializer
116-
expression with a preceding space if all the following are true:
106+
A let statement can contain an `else` component, making it a let-else statement.
107+
In this case, always apply the same formatting rules to the components preceding
108+
the `else` block (i.e. the `let pattern: Type = initializer_expr` portion)
109+
as described [for other let statements](#let-statements).
110+
111+
The entire let-else statement may be formatted on a single line if all the
112+
following are true:
113+
114+
* the entire statement is *short*
115+
* the `else` block contains only a single-line expression and no statements
116+
* the `else` block contains no comments
117+
* the let statement components preceding the `else` block can be formatted on a single line
118+
119+
```rust
120+
let Some(1) = opt else { return };
121+
```
122+
123+
Formatters may allow users to configure the value of the threshold
124+
used to determine whether a let-else statement is *short*.
125+
126+
Otherwise, the let-else statement requires some line breaks.
127+
128+
If breaking a let-else statement across multiple lines, never break between the
129+
`else` and the `{`, and always break before the `}`.
130+
131+
If the let statement components preceding the `else` can be formatted on a
132+
single line, but the let-else does not qualify to be placed entirely on a
133+
single line, put the `else {` on the same line as the initializer expression,
134+
with a space between them, then break the line after the `{`. Indent the
135+
closing `}` to match the `let`, and indent the contained block one step
136+
further.
137+
138+
```rust
139+
let Some(1) = opt else {
140+
return;
141+
};
142+
143+
let Some(1) = opt else {
144+
// nope
145+
return
146+
};
147+
```
148+
149+
If the let statement components preceding the `else` can be formatted on a
150+
single line, but the `else {` does not fit on the same line, break the line
151+
before the `else`.
152+
153+
```rust
154+
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name
155+
else {
156+
return;
157+
};
158+
```
159+
160+
If the initializer expression is multi-line, the `else` keyword and opening
161+
brace of the block (i.e. `else {`) should be put on the same line as the end of
162+
the initializer expression, with a space between them, if all the following are
163+
true:
117164

118165
* The initializer expression ends with one or more closing
119166
parentheses, square brackets, and/or braces
120167
* There is nothing else on that line
121-
* That line is not indented beyond the indent of the first line containing the `let` keyword
168+
* That line has the same indentation level as the initial `let` keyword.
122169

123170
For example:
124171

@@ -135,7 +182,9 @@ let Some(x) = y.foo(
135182
}
136183
```
137184

138-
Otherwise, the `else` keyword and opening brace should be placed on the next line after the end of the initializer expression, and should not be indented (the `else` keyword should be aligned with the `let` keyword).
185+
Otherwise, the `else` keyword and opening brace should be placed on the next
186+
line after the end of the initializer expression, and the `else` keyword should
187+
have the same indentation level as the `let` keyword.
139188

140189
For example:
141190

@@ -155,11 +204,6 @@ fn main() {
155204
return
156205
};
157206

158-
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name
159-
else {
160-
return;
161-
};
162-
163207
let Some(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =
164208
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
165209
else {
@@ -168,31 +212,6 @@ fn main() {
168212
}
169213
```
170214

171-
##### Single line let-else statements
172-
173-
The entire let-else statement may be formatted on a single line if all the following are true:
174-
175-
* the entire statement is *short*
176-
* the `else` block contains a single-line expression and no statements
177-
* the `else` block contains no comments
178-
* the let statement components preceding the `else` block can be formatted on a single line
179-
180-
```rust
181-
let Some(1) = opt else { return };
182-
183-
let Some(1) = opt else {
184-
return;
185-
};
186-
187-
let Some(1) = opt else {
188-
// nope
189-
return
190-
};
191-
```
192-
193-
Formatters may allow users to configure the value of the threshold
194-
used to determine whether a let-else statement is *short*.
195-
196215
### Macros in statement position
197216

198217
A macro use in statement position should use parentheses or square brackets as

0 commit comments

Comments
 (0)