Skip to content

Commit 488f639

Browse files
committed
Blocks
closes #21 cc #89
1 parent cde4782 commit 488f639

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

guide/expressions.md

+115
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
11
## Expressions
22

3+
### Blocks
4+
5+
A block expression should have a newline after the initial `{` and before the
6+
terminal `}`. Any qualifier before the block (e.g., `unsafe`) should always be
7+
on the same line as the opening brace, and separated with a single space. The
8+
contents of the block should be block indented:
9+
10+
```
11+
fn block_as_stmt() {
12+
a_call();
13+
14+
{
15+
a_call_inside_a_block();
16+
17+
// a comment in a block
18+
the_value
19+
}
20+
}
21+
22+
fn block_as_expr() {
23+
let foo = {
24+
a_call_inside_a_block();
25+
26+
// a comment in a block
27+
the_value
28+
};
29+
}
30+
31+
fn unsafe_block_as_stmt() {
32+
a_call();
33+
34+
unsafe {
35+
a_call_inside_a_block();
36+
37+
// a comment in a block
38+
the_value
39+
}
40+
}
41+
```
42+
43+
If a block has an attribute, it should be on its own line:
44+
45+
```
46+
fn block_as_stmt() {
47+
#[an_attribute]
48+
{
49+
#![an_inner_attribute]
50+
51+
// a comment in a block
52+
the_value
53+
}
54+
}
55+
```
56+
57+
Avoid writing comments on the same line as the braces.
58+
59+
An empty block should be written as `{}`.
60+
61+
A block may be written on a single line if:
62+
63+
* it is either
64+
- used in expression position (not statement position)
65+
- is an unsafe block in statement position
66+
* contains a single-line expression and no statements
67+
* contains no comments
68+
69+
A single line block should have spaces after the opening brace and before the
70+
closing brace.
71+
72+
Examples:
73+
74+
```
75+
fn main() {
76+
// Single line
77+
let _ = { a_call() };
78+
let _ = unsafe { a_call() };
79+
80+
// Not allowed on one line
81+
// Statement position.
82+
{
83+
a_call()
84+
}
85+
86+
// Contains a statement
87+
let _ = {
88+
a_call();
89+
};
90+
unsafe {
91+
a_call();
92+
}
93+
94+
// Contains a comment
95+
let _ = {
96+
// A comment
97+
};
98+
let _ = {
99+
// A comment
100+
a_call()
101+
};
102+
103+
// Multiple lines
104+
let _ = {
105+
a_call();
106+
another_call()
107+
};
108+
let _ = {
109+
a_call(
110+
an_argument,
111+
another_arg,
112+
)
113+
};
114+
}
115+
```
116+
117+
3118
### Closures
4119

5120
Don't put any extra spaces before the first `|` (unless the closure is prefixed

0 commit comments

Comments
 (0)