Could somebody explain, why if I remove the "move" from for_each, the program hangs?
use std::sync::mpsc::channel;
fn main() {
let v = vec![0,1,2,3,4,5,6,7,8,9];
let (tx, rx) = channel();
v.iter().for_each(move |&elem| {
tx.send(elem).unwrap();
});
println!("Tx'ed");
let rx_vec: Vec<_> = rx.iter().collect();
println!("Collected into vec");
}
>Solution :
The code hangs without move because in that variant tx never gets dropped so the channel isn’t closed so the rx waits indefinitely for additional data, you can just drop(tx) before rx.iter() to close the channel.