Skip to content

Commit 108e5d8

Browse files
authored
Merge pull request #97 from rust-lang-nursery/guide-match
Guide - match
2 parents 84a7dac + dca56fe commit 108e5d8

File tree

1 file changed

+162
-2
lines changed

1 file changed

+162
-2
lines changed

guide/expressions.md

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Do not put a comma after the last argument.
124124
foo(x, y, z)
125125
```
126126

127-
### Methods
127+
### Method calls
128128

129129
Follow the function rules for calling.
130130

@@ -142,4 +142,164 @@ Put spaces before and after `as`:
142142

143143
```rust
144144
let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char;
145-
```
145+
```
146+
147+
148+
### Match
149+
150+
Prefer not to line-break inside the discriminant expression. There must always
151+
be a line break after the opening brace and before the closing brace. The match
152+
arms must be block indented once:
153+
154+
```rust
155+
match foo {
156+
// arms
157+
}
158+
159+
let x = match foo.bar.baz() {
160+
// arms
161+
};
162+
```
163+
164+
Use a trailing comma for a match arm if and only if not using a block.
165+
166+
Avoid splitting the left-hand side (before the `=>`) of a match arm where
167+
possible. If the right-hand side of the match arm is kept on the same line,
168+
never use a block (unless the block is empty).
169+
170+
If the right-hand side consists of multiple statements or has line comments or
171+
the start of the line cannot be fit on the same line as the left-hand side, use
172+
a block.
173+
174+
The body of a block arm should be block indented once.
175+
176+
Examples:
177+
178+
```rust
179+
match foo {
180+
foo => bar,
181+
a_very_long_patten | another_pattern if an_expression() => {
182+
no_room_for_this_expression()
183+
}
184+
foo => {
185+
// A comment.
186+
an_expression()
187+
}
188+
foo => {
189+
let a = statement();
190+
an_expression()
191+
}
192+
bar => {}
193+
// Trailing comma on last item.
194+
foo => bar,
195+
}
196+
```
197+
198+
If the body is a single expression with no line comments is a *combinable
199+
expression* (see below for details), then it may be started on the same line as
200+
the right-hand side. If not, then it must be in a block. Example,
201+
202+
```rust
203+
match foo {
204+
// A combinable expression.
205+
foo => a_function_call(another_call(
206+
argument1,
207+
argument2,
208+
)),
209+
// A non-combinable expression
210+
bar => {
211+
a_function_call(
212+
another_call(
213+
argument1,
214+
argument2,
215+
),
216+
another_argument,
217+
)
218+
}
219+
}
220+
```
221+
222+
#### Line-breaking
223+
224+
Where it is possible to use a block form on the right-hand side and avoid
225+
breaking the left-hand side, do that. E.g.
226+
227+
```rust
228+
// Assuming the following line does done fit in the max width
229+
a_very_long_pattern | another_pattern => ALongStructName {
230+
...
231+
},
232+
// Prefer this
233+
a_very_long_pattern | another_pattern => {
234+
ALongStructName {
235+
...
236+
}
237+
}
238+
// To splitting the pattern.
239+
```
240+
241+
Never break after `=>` without using the block form of the body.
242+
243+
If the left-hand side must be split and there is an `if` clause, break before
244+
the `if` and block indent. In this case, always use a block body and start the
245+
body on a new line:
246+
247+
```rust
248+
a_very_long_pattern | another_pattern
249+
if expr =>
250+
{
251+
...
252+
}
253+
```
254+
255+
If required to break the pattern, put each clause of the pattern on its own
256+
line, breaking before the `|`. If there is an `if` clause, then you must use the
257+
above form:
258+
259+
```rust
260+
a_very_long_pattern
261+
| another_pattern
262+
| yet_another_pattern
263+
| a_forth_pattern => {
264+
...
265+
}
266+
a_very_long_pattern
267+
| another_pattern
268+
| yet_another_pattern
269+
| a_forth_pattern
270+
if expr =>
271+
{
272+
...
273+
}
274+
```
275+
276+
If every clause in a pattern is *small*, but does not fit on one line, then the
277+
pattern may be formatted across multiple lines with as many clauses per line as
278+
possible. Again break before a `|`:
279+
280+
```rust
281+
foo | bar | baz
282+
| qux => {
283+
...
284+
}
285+
```
286+
287+
We define a pattern clause to be *small* if it matches the following grammar:
288+
289+
```
290+
[small, ntp]:
291+
- single token
292+
- `&[single-line, ntp]`
293+
294+
[small]:
295+
- `[small, ntp]`
296+
- unary tuple constructor `([small, ntp])`
297+
- `&[small]`
298+
```
299+
300+
E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
301+
302+
303+
### Combinable expressions
304+
305+
TODO (#61)

0 commit comments

Comments
 (0)