std::sync::mpsc — multi-producer, single-consumer.
Basic:
1use std::sync::mpsc;2use std::thread;34let (tx, rx) = mpsc::channel();56for i in 0..10 {7 let tx = tx.clone();8 thread::spawn(move || {9 tx.send(i).unwrap();10 });11}1213drop(tx);1415for received in rx {16 println!("Got: {}", received);17}
Tokio channels:
1use tokio::sync::mpsc;23let (tx, mut rx) = mpsc::channel(32);45tokio::spawn(async move {6 tx.send("hello").await.unwrap();7});89let msg = rx.recv().await.unwrap();
Key: mpsc for threads, tokio channels for async.