Skip to content

Commit

Permalink
Closures
Browse files Browse the repository at this point in the history
closes #35
  • Loading branch information
nrc committed Feb 5, 2018
1 parent 3806502 commit 3a6d441
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions guide/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,35 @@ by `move`), but put a space between the second `|` and the expression of the
closure. Between the `|`s, you should use function definition syntax, however,
elide types where possible.

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

```rust
|arg1, arg2| expr

move |arg1: i32, arg2: i32| -> i32 { expr1; expr2 }
move |arg1: i32, arg2: i32| -> i32 {
expr1;
expr2
}

|| Foo {
field1,
field2: 0,
}

|| {
if true {
blah
} else {
boo
}
}

|x| unsafe {
expr
}
```

### Array literals
Expand Down

0 comments on commit 3a6d441

Please sign in to comment.