Message Passing

use std::sync::mpsc;
use std::thread;
use std::time::Duration;

fn main() {

    let (tx, rx) = mpsc::channel();

    let tx1 = mpsc::Sender::clone(&tx);

    thread::spawn(move || {
    
        let vs = vec![
            String::from("a1"),
            String::from("a2"),
            String::from("a3"),
        ];

        for v in vs {
    
            tx1.send(v).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });

    thread::spawn(move || {
    
        let vs = vec![
            String::from("b1"),
            String::from("b2"),
            String::from("b3"),
        ];

        for v in vs {
   
            tx.send(v).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });

    //for received in rx.iter().skip(1) {
    for received in rx {
   
        println!("Got: {}", received);
    }
}