Skip to content

Commit dca56fe

Browse files
committed
Guide - match
Closes #34 cc #61, #89
1 parent 6c702c4 commit dca56fe

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
@@ -66,7 +66,7 @@ Do not put a comma after the last argument.
6666
foo(x, y, z)
6767
```
6868

69-
### Methods
69+
### Method calls
7070

7171
Follow the function rules for calling.
7272

@@ -84,4 +84,164 @@ Put spaces before and after `as`:
8484

8585
```rust
8686
let cstr = "Hi\0" as *const str as *const [u8] as *const std::os::raw::c_char;
87-
```
87+
```
88+
89+
90+
### Match
91+
92+
Prefer not to line-break inside the discriminant expression. There must always
93+
be a line break after the opening brace and before the closing brace. The match
94+
arms must be block indented once:
95+
96+
```rust
97+
match foo {
98+
// arms
99+
}
100+
101+
let x = match foo.bar.baz() {
102+
// arms
103+
};
104+
```
105+
106+
Use a trailing comma for a match arm if and only if not using a block.
107+
108+
Avoid splitting the left-hand side (before the `=>`) of a match arm where
109+
possible. If the right-hand side of the match arm is kept on the same line,
110+
never use a block (unless the block is empty).
111+
112+
If the right-hand side consists of multiple statements or has line comments or
113+
the start of the line cannot be fit on the same line as the left-hand side, use
114+
a block.
115+
116+
The body of a block arm should be block indented once.
117+
118+
Examples:
119+
120+
```rust
121+
match foo {
122+
foo => bar,
123+
a_very_long_patten | another_pattern if an_expression() => {
124+
no_room_for_this_expression()
125+
}
126+
foo => {
127+
// A comment.
128+
an_expression()
129+
}
130+
foo => {
131+
let a = statement();
132+
an_expression()
133+
}
134+
bar => {}
135+
// Trailing comma on last item.
136+
foo => bar,
137+
}
138+
```
139+
140+
If the body is a single expression with no line comments is a *combinable
141+
expression* (see below for details), then it may be started on the same line as
142+
the right-hand side. If not, then it must be in a block. Example,
143+
144+
```rust
145+
match foo {
146+
// A combinable expression.
147+
foo => a_function_call(another_call(
148+
argument1,
149+
argument2,
150+
)),
151+
// A non-combinable expression
152+
bar => {
153+
a_function_call(
154+
another_call(
155+
argument1,
156+
argument2,
157+
),
158+
another_argument,
159+
)
160+
}
161+
}
162+
```
163+
164+
#### Line-breaking
165+
166+
Where it is possible to use a block form on the right-hand side and avoid
167+
breaking the left-hand side, do that. E.g.
168+
169+
```rust
170+
// Assuming the following line does done fit in the max width
171+
a_very_long_pattern | another_pattern => ALongStructName {
172+
...
173+
},
174+
// Prefer this
175+
a_very_long_pattern | another_pattern => {
176+
ALongStructName {
177+
...
178+
}
179+
}
180+
// To splitting the pattern.
181+
```
182+
183+
Never break after `=>` without using the block form of the body.
184+
185+
If the left-hand side must be split and there is an `if` clause, break before
186+
the `if` and block indent. In this case, always use a block body and start the
187+
body on a new line:
188+
189+
```rust
190+
a_very_long_pattern | another_pattern
191+
if expr =>
192+
{
193+
...
194+
}
195+
```
196+
197+
If required to break the pattern, put each clause of the pattern on its own
198+
line, breaking before the `|`. If there is an `if` clause, then you must use the
199+
above form:
200+
201+
```rust
202+
a_very_long_pattern
203+
| another_pattern
204+
| yet_another_pattern
205+
| a_forth_pattern => {
206+
...
207+
}
208+
a_very_long_pattern
209+
| another_pattern
210+
| yet_another_pattern
211+
| a_forth_pattern
212+
if expr =>
213+
{
214+
...
215+
}
216+
```
217+
218+
If every clause in a pattern is *small*, but does not fit on one line, then the
219+
pattern may be formatted across multiple lines with as many clauses per line as
220+
possible. Again break before a `|`:
221+
222+
```rust
223+
foo | bar | baz
224+
| qux => {
225+
...
226+
}
227+
```
228+
229+
We define a pattern clause to be *small* if it matches the following grammar:
230+
231+
```
232+
[small, ntp]:
233+
- single token
234+
- `&[single-line, ntp]`
235+
236+
[small]:
237+
- `[small, ntp]`
238+
- unary tuple constructor `([small, ntp])`
239+
- `&[small]`
240+
```
241+
242+
E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
243+
244+
245+
### Combinable expressions
246+
247+
TODO (#61)

0 commit comments

Comments
 (0)