An mpsc channel is a thread-safe communication path: many sending endpoints can send values to one receiving endpoint. Rust’s standard library keeps the Sender and Receiver connected even when they live in different functions, structs, or threads. section 12.5 section 12.5.1
A channel follows a small set of practical rules:
- Create one with std::sync::mpsc::channel(), which returns a Sender and Receiver carrying the same generic type. section 12.5.1
- Send values with Sender::send and receive them with Receiver::recv; both methods return Result values because either endpoint can be unavailable. section 12.5.2
- Clone the Sender to give multiple threads their own sending endpoint, while keeping one Receiver to collect their messages. section 12.5.2
- Treat recv as blocking: it waits for a value while a Sender remains alive, whereas try_recv checks immediately and may find nothing. section 12.5.2
- Expect sending to fail after the Receiver is dropped, and expect receiving to return an error after all Senders are dropped and queued data is exhausted. section 12.5.2
Two senders, one receiver
Create a channel, clone its Sender, and move each sender into a separate thread. Each thread sends strings, while the receiver loops with while let Ok(message) = receiver.recv() and prints every message until the senders are gone. The messages arrive in the order the Receiver gets them, not as a separately coordinated sequence. section 12.5.2
use std::sync::mpsc::channel;
fn main() {
let (sender, receiver) = channel();
let sender_clone = sender.clone();
std::thread::spawn(move || {
sender.send("Send a &str this time").unwrap(); #1
sender.send("Send a &str this time").unwrap(); #1
});
std::thread::spawn(move || {
sender_clone.send("And here is another &str").unwrap(); #2
sender_clone.send("And here is another &str").unwrap(); #2
});
while let Ok(res) = receiver.recv() {
println!("{res}");
}
}
Use the complete threaded example to inspect Sender cloning, move closures, repeated sends, and the receiving loop together. section 12.5.2
