std-method

Sender::clone: multiple producers

Clone the sending endpoint, not the queue or its receiver. All producers feed the same receiver. Across independently scheduled workers, do not assume which producer arrives first. Expected stdout (not executed): [3, 8] Common mistake: Assuming worker launch order determines arrival order.

Formal Syntax & Structure

rust syntaxrust
let another = sender.clone();

Working Example

Example Snippetrust
fn main() {    use std::{sync::mpsc, thread};    let (tx, rx) = mpsc::channel();    let another = tx.clone();    let a = thread::spawn(move || tx.send(8).unwrap());    let b = thread::spawn(move || another.send(3).unwrap());    let mut received: Vec<_> = rx.into_iter().collect();    a.join().unwrap(); b.join().unwrap();    received.sort();    println!("{:?}", received);}
Rust Reference