Skip to content

Commit 94c02af

Browse files
committed
Add section on closure types to manual
1 parent 61443dc commit 94c02af

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/rust.md

+26
Original file line numberDiff line numberDiff line change
@@ -3193,6 +3193,32 @@ let bo: Binop = add;
31933193
x = bo(5,7);
31943194
~~~~
31953195

3196+
### Closure types
3197+
3198+
The type of a closure mapping an input of type `A` to an output of type `B` is `|A| -> B`. A closure with no arguments or return values has type `||`.
3199+
3200+
3201+
An example of creating and calling a closure:
3202+
3203+
```rust
3204+
let captured_var = 10;
3205+
3206+
let closure_no_args = || println!("captured_var={}", captured_var);
3207+
3208+
let closure_args = |arg: int| -> int {
3209+
println!("captured_var={}, arg={}", captured_var, arg);
3210+
arg // Note lack of semicolon after 'arg'
3211+
};
3212+
3213+
fn call_closure(c1: ||, c2: |int| -> int) {
3214+
c1();
3215+
c2(2);
3216+
}
3217+
3218+
call_closure(closure_no_args, closure_args);
3219+
3220+
```
3221+
31963222
### Object types
31973223

31983224
Every trait item (see [traits](#traits)) defines a type with the same name as the trait.

0 commit comments

Comments
 (0)