Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section on closure types to manual #10773

Merged
merged 1 commit into from
Dec 3, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,32 @@ let bo: Binop = add;
x = bo(5,7);
~~~~

### Closure types

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 `||`.


An example of creating and calling a closure:

```rust
let captured_var = 10;

let closure_no_args = || println!("captured_var={}", captured_var);

let closure_args = |arg: int| -> int {
println!("captured_var={}, arg={}", captured_var, arg);
arg // Note lack of semicolon after 'arg'
};

fn call_closure(c1: ||, c2: |int| -> int) {
c1();
c2(2);
}

call_closure(closure_no_args, closure_args);

```

### Object types

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