@@ -3110,10 +3110,12 @@ the lambda expression captures its environment by reference, effectively
3110
3110
borrowing pointers to all outer variables mentioned inside the function.
3111
3111
Alternately, the compiler may infer that a lambda expression should copy or
3112
3112
move values (depending on their type) from the environment into the lambda
3113
- expression's captured environment.
3113
+ expression's captured environment. A lambda can be forced to capture its
3114
+ environment by moving values by prefixing it with the ` move ` keyword.
3114
3115
3115
3116
In this example, we define a function ` ten_times ` that takes a higher-order
3116
- function argument, and we then call it with a lambda expression as an argument:
3117
+ function argument, and we then call it with a lambda expression as an argument,
3118
+ followed by a lambda expression that moves values from its environment.
3117
3119
3118
3120
```
3119
3121
fn ten_times<F>(f: F) where F: Fn(i32) {
@@ -3123,6 +3125,9 @@ fn ten_times<F>(f: F) where F: Fn(i32) {
3123
3125
}
3124
3126
3125
3127
ten_times(|j| println!("hello, {}", j));
3128
+
3129
+ let word = "konnichiwa".to_owned();
3130
+ ten_times(move |j| println!("{}, {}", word, j));
3126
3131
```
3127
3132
3128
3133
### Infinite loops
@@ -3961,12 +3966,12 @@ implementation in the returned type `U`.
3961
3966
3962
3967
## The ` Send ` trait
3963
3968
3964
- The ` Send ` trait indicates that a value of this type is safe to send from one
3969
+ The ` Send ` trait indicates that a value of this type is safe to send from one
3965
3970
thread to another.
3966
3971
3967
- ## The ' Sync' trait
3972
+ ## The ` Sync ` trait
3968
3973
3969
- The ' Sync' trait indicates that a value of this type is safe to share between
3974
+ The ` Sync ` trait indicates that a value of this type is safe to share between
3970
3975
multiple threads.
3971
3976
3972
3977
# Memory model
0 commit comments