You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/style-guide/src/statements.md
+78
Original file line number
Diff line number
Diff line change
@@ -99,6 +99,84 @@ let Foo {
99
99
);
100
100
```
101
101
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
+
letSome(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
+
letSome(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
+
letSome(1) =optelse { return };
167
+
168
+
letSome(1) =optelse {
169
+
return;
170
+
};
171
+
172
+
letSome(1) =optelse {
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*.
0 commit comments