Add closure move sample.

pull/594/head
Rustln 3 years ago committed by GitHub
parent 5907d78a49
commit 73742bb3bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -421,6 +421,15 @@ false
如果你想强制闭包取得捕获变量的所有权,可以在参数列表前添加 `move` 关键字,这种用法通常用于闭包的生命周期大于捕获变量的生命周期时,例如将闭包返回或移入其他线程。
```rust
use std::thread;
let v = vec![1, 2, 3];
let handle = thread::spawn(move || {
println!("Here's a vector: {:?}", v);
});
handle.join().unwrap();
```
2. `FnMut`,它以可变借用的方式捕获了环境中的值,因此可以修改该值:
```rust

Loading…
Cancel
Save