Here's the code:
use std::thread;
use std::sync::mpsc;
fn main() {
//spawn threads
let (tx, rx) = mpsc::channel();
for mut i in 0 .. 10 {
let txc = tx.clone(); //clone from the main sender
thread::spawn( move || {
i += 20;
println!("Sending: {}", i);
txc.send(i).unwrap_or_else(|e| {
eprintln!("{}", e);
});
});
}
for received in rx {
println!("Received: {}", received);
}
}
The code runs successfully but it hangs and the process never exits at the end.
I thought it could be related to closing the channel ends and I tried dropping by tx.drop()
and rx.drop()
but the compiler gave an error.
What am I doing wrong here?