Skip to content

Reference: Mention move keyword for lambdas #36929

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

Merged
merged 1 commit into from
Oct 7, 2016
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
9 changes: 7 additions & 2 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3110,10 +3110,12 @@ the lambda expression captures its environment by reference, effectively
borrowing pointers to all outer variables mentioned inside the function.
Alternately, the compiler may infer that a lambda expression should copy or
move values (depending on their type) from the environment into the lambda
expression's captured environment.
expression's captured environment. A lambda can be forced to capture its
environment by moving values by prefixing it with the `move` keyword.

In this example, we define a function `ten_times` that takes a higher-order
function argument, and we then call it with a lambda expression as an argument:
function argument, and we then call it with a lambda expression as an argument,
followed by a lambda expression that moves values from its environment.

```
fn ten_times<F>(f: F) where F: Fn(i32) {
Expand All @@ -3123,6 +3125,9 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
}

ten_times(|j| println!("hello, {}", j));

let word = "konnichiwa".to_owned();
ten_times(move |j| println!("{}, {}", word, j));
```

### Infinite loops
Expand Down