use std::sync::mpsc::channel;
use std::thread;

fn main() {
    let (sender, receiver) = channel::<Option<i32>>();

    let h = thread::spawn(move || {
        loop {
            match receiver.recv() {
                Ok(o) => match o {
                    None => {
                        println!("The end!");
                        return;
                    }
                    Some(i) => println!("Received: {}", i),
                }
                Err(e) => { // when sender closed
                    println!("Error: {}", e);
                    return;
                }
            }
        }
    });

    sender.send(Some(1)).unwrap();
    sender.send(Some(2)).unwrap();
    sender.send(Some(3)).unwrap();
    sender.send(None).unwrap();

    h.join().unwrap();
}