Skip to content

Commit b551730

Browse files
committed
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.
1 parent 065a1f5 commit b551730

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
@@ -101,22 +101,69 @@ let Foo {
101101

102102
#### else blocks (let-else statements)
103103

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

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

121168
For example:
122169

@@ -133,7 +180,9 @@ let Some(x) = y.foo(
133180
}
134181
```
135182

136-
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).
183+
Otherwise, the `else` keyword and opening brace should be placed on the next
184+
line after the end of the initializer expression, and the `else` keyword should
185+
have the same indentation level as the `let` keyword.
137186

138187
For example:
139188

@@ -153,11 +202,6 @@ fn main() {
153202
return
154203
};
155204

156-
let Some(x) = some_really_really_really_really_really_really_really_really_really_long_name
157-
else {
158-
return;
159-
};
160-
161205
let Some(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =
162206
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
163207
else {
@@ -166,31 +210,6 @@ fn main() {
166210
}
167211
```
168212

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

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

0 commit comments

Comments
 (0)