Canais Ilimitados

VocĂȘ obtĂ©m um canal ilimitado e assĂ­ncrono com mpsc::channel():

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

fn main() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        let thread_id = thread::current().id();
        for i in 1..10 {
            tx.send(format!("Mensagem {i}")).unwrap();
            println!("{thread_id:?}: Mensagem {i} enviada");
        }
        println!("{thread_id:?}: terminado");
    });
    thread::sleep(Duration::from_millis(100));

    for msg in rx.iter() {
        println!("Main: obteve {msg}");
    }
}