You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let job = receiver.lock().unwrap().recv().unwrap();
1530
+
let thread = thread::spawn(move || {
1531
+
loop {
1532
+
let job = receiver.lock().unwrap().recv().unwrap();
1521
1533
1522
-
println!("Worker {id} got a job; executing.");
1534
+
println!("Worker {id} got a job; executing.");
1523
1535
1524
-
job();
1536
+
job();
1537
+
}
1525
1538
});
1526
1539
1527
1540
Worker { id, thread }
@@ -1650,7 +1663,7 @@ longer than intended if we aren’t mindful of the lifetime of the
1650
1663
`MutexGuard<T>`.
1651
1664
1652
1665
The code in Listing 21-20 that uses `let job = receiver.lock().unwrap().recv().unwrap();` works because with `let`, any
1653
-
temporary values used in the expression on the right-hand side of the equal
1666
+
temporary values used in the expression on the righthand side of the equal
1654
1667
sign are immediately dropped when the `let` statement ends. However, `while let` (and `if let` and `match`) does not drop temporary values until the end of
1655
1668
the associated block. In Listing 21-21, the lock remains held for the duration
1656
1669
of the call to `job()`, meaning other `Worker` instances cannot receive jobs.
@@ -1712,18 +1725,15 @@ Here is the error we get when we compile this code:
1712
1725
$ cargo check
1713
1726
Checking hello v0.1.0 (file:///projects/hello)
1714
1727
error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference
1715
-
--> src/lib.rs:52:13
1716
-
|
1717
-
52 | worker.thread.join().unwrap();
1718
-
| ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call
1719
-
| |
1720
-
| move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
1721
-
|
1728
+
--> src/lib.rs:52:13
1729
+
|
1730
+
52 | worker.thread.join().unwrap();
1731
+
| ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call
1732
+
| |
1733
+
| move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
1734
+
|
1722
1735
note: `JoinHandle::<T>::join` takes ownership of the receiver `self`, which moves `worker.thread`
0 commit comments