Skip to content

Commit f1ef36e

Browse files
committed
auto merge of #10773 : jvns/rust/patch-1, r=cmr
The section on closure types was missing, so I added one. I'm new to Rust, so there are probably important things to say about closure types that I'm missing here. I tested the example with the latest Rust nightly.
2 parents 18084be + 94c02af commit f1ef36e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/rust.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,32 @@ let bo: Binop = add;
32253225
x = bo(5,7);
32263226
~~~~
32273227

3228+
### Closure types
3229+
3230+
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 `||`.
3231+
3232+
3233+
An example of creating and calling a closure:
3234+
3235+
```rust
3236+
let captured_var = 10;
3237+
3238+
let closure_no_args = || println!("captured_var={}", captured_var);
3239+
3240+
let closure_args = |arg: int| -> int {
3241+
println!("captured_var={}, arg={}", captured_var, arg);
3242+
arg // Note lack of semicolon after 'arg'
3243+
};
3244+
3245+
fn call_closure(c1: ||, c2: |int| -> int) {
3246+
c1();
3247+
c2(2);
3248+
}
3249+
3250+
call_closure(closure_no_args, closure_args);
3251+
3252+
```
3253+
32283254
### Object types
32293255

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

0 commit comments

Comments
 (0)