mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-31 07:49:18 +00:00
fix: revert exercise to unsolved state
This commit is contained in:
parent
2e9462fe1a
commit
0df7d4b9dc
@ -15,28 +15,20 @@ impl Queue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) {
|
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) {
|
||||||
// Destructure the Queue to move first_half and second_half independently
|
// TODO: We want to send `tx` to both threads. But currently, it is moved
|
||||||
let Queue {
|
// into the first thread. How could you solve this problem?
|
||||||
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 || {
|
thread::spawn(move || {
|
||||||
for val in first_half {
|
for val in q.first_half {
|
||||||
println!("Sending {val:?}");
|
println!("Sending {val:?}");
|
||||||
tx1.send(val).unwrap();
|
tx.send(val).unwrap();
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
for val in second_half {
|
for val in q.second_half {
|
||||||
println!("Sending {val:?}");
|
println!("Sending {val:?}");
|
||||||
tx2.send(val).unwrap();
|
tx.send(val).unwrap();
|
||||||
thread::sleep(Duration::from_millis(250));
|
thread::sleep(Duration::from_millis(250));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -65,4 +57,4 @@ mod tests {
|
|||||||
received.sort();
|
received.sort();
|
||||||
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
assert_eq!(received, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user