mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2024-11-14 04:41:49 +08:00
27 lines
1.1 KiB
Plaintext
27 lines
1.1 KiB
Plaintext
$ cargo run
|
|
Compiling shared-state v0.1.0 (file:///projects/shared-state)
|
|
error[E0382]: borrow of moved value: `counter`
|
|
--> src/main.rs:21:29
|
|
|
|
|
5 | let counter = Mutex::new(0);
|
|
| ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
|
|
...
|
|
8 | for _ in 0..10 {
|
|
| -------------- inside of this loop
|
|
9 | let handle = thread::spawn(move || {
|
|
| ------- value moved into closure here, in previous iteration of loop
|
|
...
|
|
21 | println!("Result: {}", *counter.lock().unwrap());
|
|
| ^^^^^^^ value borrowed here after move
|
|
|
|
|
help: consider moving the expression out of the loop so it is only moved once
|
|
|
|
|
8 ~ let mut value = counter.lock();
|
|
9 ~ for _ in 0..10 {
|
|
10 | let handle = thread::spawn(move || {
|
|
11 ~ let mut num = value.unwrap();
|
|
|
|
|
|
|
For more information about this error, try `rustc --explain E0382`.
|
|
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
|