Skip to content

Commit 10b9c1d

Browse files
docs(style-guide): add rules for let-else statements
1 parent c18a5e8 commit 10b9c1d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

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

+78
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,84 @@ let Foo {
9999
);
100100
```
101101

102+
#### else blocks (let-else statements)
103+
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:
115+
116+
* The initializer expression ends with one or more closing
117+
parentheses, square brackets, and/or braces
118+
* There is nothing else on that line
119+
* That line is not indented beyond the indent of the first line containing the `let` keyword
120+
121+
For example:
122+
123+
```rust
124+
let Some(x) = y.foo(
125+
"abc",
126+
fairly_long_identifier,
127+
"def",
128+
"123456",
129+
"string",
130+
"cheese",
131+
) else {
132+
bar()
133+
}
134+
```
135+
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).
137+
138+
For example:
139+
140+
```rust
141+
let Some(x) = abcdef()
142+
.foo(
143+
"abc",
144+
some_really_really_really_long_ident,
145+
"ident",
146+
"123456",
147+
)
148+
.bar()
149+
.baz()
150+
.qux("fffffffffffffffff")
151+
else {
152+
foo_bar()
153+
}
154+
```
155+
156+
##### Single line let-else statements
157+
158+
The entire let-else statement may be formatted on a single line if all the following are true:
159+
160+
* the entire statement is *short*
161+
* the `else` block contains a single-line expression and no statements
162+
* the `else` block contains no comments
163+
* the let statement components preceding the `else` block can be formatted on a single line
164+
165+
```rust
166+
let Some(1) = opt else { return };
167+
168+
let Some(1) = opt else {
169+
return;
170+
};
171+
172+
let Some(1) = opt else {
173+
// nope
174+
return
175+
};
176+
```
177+
178+
Formatters may allow users to configure the value of the threshold
179+
used to determine whether a let-else statement is *short*.
102180

103181
### Macros in statement position
104182

0 commit comments

Comments
 (0)