diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2016-10-07 11:45:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-10-07 11:45:04 +0200 |
| commit | bf41e9fee27870ae9d5e129581f80f67099d4333 (patch) | |
| tree | 4ffd088483994a8f71f0aa2710d04f9df08d7473 | |
| parent | c75001b2af7b7afab13850eaad0f83562d5d8195 (diff) | |
| parent | 58190ccb634ae7931fd6689e1a2c3b5382843ec9 (diff) | |
Rollup merge of #36929 - angelsl:issue-36683, r=steveklabnik
Reference: Mention `move` keyword for lambdas From issue #36683
| -rw-r--r-- | src/doc/reference.md | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index 20970ab7a35..b57104fc6f5 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -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) { @@ -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 |
