Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/19_smart_pointers/cow1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ mod tests {

#[test]
fn owned_mutation() {
// Of course this is also the case if a mutation does occur (not all
// numbers are absolute). In this case, the call to `to_mut()` in the
// `abs_all` function returns a reference to the same data as before.
// When the data is already owned, even if a mutation occurs, `to_mut()`
// simply returns a mutable reference to the existing owned data without
// performing any clone.
let vec = vec![-1, 0, 1];
let mut input = Cow::from(vec);
abs_all(&mut input);
Expand Down
22 changes: 14 additions & 8 deletions solutions/20_threads/threads3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@ impl Queue {
}

fn send_tx(q: Queue, tx: mpsc::Sender<u32>) {
// Clone the sender `tx` first.
let tx_clone = tx.clone();
// Destructure the Queue to move first_half and second_half independently
let Queue {
first_half,
second_half,
} = q;

// Clone the sender so both threads can send to the same receiver
let tx1 = tx.clone();
let tx2 = tx;

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.
tx_clone.send(val).unwrap();
tx1.send(val).unwrap();
thread::sleep(Duration::from_millis(250));
}
});

thread::spawn(move || {
for val in q.second_half {
for val in second_half {
println!("Sending {val:?}");
tx.send(val).unwrap();
tx2.send(val).unwrap();
thread::sleep(Duration::from_millis(250));
}
});
Expand Down
Loading