Skip to content

Commit 0205f37

Browse files
committed
Closures
closes #35
1 parent 08904c2 commit 0205f37

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

guide/expressions.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,35 @@ by `move`), but put a space between the second `|` and the expression of the
122122
closure. Between the `|`s, you should use function definition syntax, however,
123123
elide types where possible.
124124

125-
Use closures without the enclosing `{}`, if possible. Add the `{}` when you
126-
have a return type, when there are statements before the final expression, or
127-
when you need to split it into multiple lines; examples:
125+
Use closures without the enclosing `{}`, if possible. Add the `{}` when you have
126+
a return type, when there are statements, there are comments in the body, or the
127+
body expression spans multiple lines and is a control-flow expression. If using
128+
braces, follow the rules above for blocks. Examples:
128129

129130
```rust
130131
|arg1, arg2| expr
131132

132-
move |arg1: i32, arg2: i32| -> i32 { expr1; expr2 }
133+
move |arg1: i32, arg2: i32| -> i32 {
134+
expr1;
135+
expr2
136+
}
137+
138+
|| Foo {
139+
field1,
140+
field2: 0,
141+
}
142+
143+
|| {
144+
if true {
145+
blah
146+
} else {
147+
boo
148+
}
149+
}
150+
151+
|x| unsafe {
152+
expr
153+
}
133154
```
134155

135156
### Array literals

0 commit comments

Comments
 (0)