Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Program hangs on channel rx

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading