diff --git a/solutions/20_threads/threads3.rs b/solutions/20_threads/threads3.rs index 7ceefea0..a345b44c 100644 --- a/solutions/20_threads/threads3.rs +++ b/solutions/20_threads/threads3.rs @@ -17,8 +17,14 @@ impl Queue { fn send_tx(q: Queue, tx: mpsc::Sender) { // Clone the sender `tx` first. let tx_clone = tx.clone(); + + // Defining the data in new variables. + // `q` cannot be moved simultaneously into multiple places. + let first_half = q.first_half; + let second_half = q.second_half; + thread::spawn(move || { - for val in q.first_half { + for val in first_half { println!("Sending {val:?}"); // Then use the clone in the first thread. This means that // `tx_clone` is moved to the first thread and `tx` to the second. @@ -28,7 +34,7 @@ fn send_tx(q: Queue, tx: mpsc::Sender) { }); thread::spawn(move || { - for val in q.second_half { + for val in second_half { println!("Sending {val:?}"); tx.send(val).unwrap(); thread::sleep(Duration::from_millis(250));