Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
use std::sync::mpsc; use std::thread; use std::time::Duration; fn main() { // --- Basic channel --- let (tx, rx) = mpsc::channel::<String>(); let tx_clone = tx.clone(); thread::spawn(move || { tx.send("message from thread 1".to_string()).unwrap(); }); thread::spawn(move || { tx_clone.send("message from thread 2".to_string()).unwrap(); }); // recv blocks until a message arrives for _ in 0..2 { println!("received: {}", rx.recv().unwrap()); } // --- Pipeline with worker threads --- let (jobs_tx, jobs_rx) = mpsc::channel::<u64>(); let (results_tx, results_rx) = mpsc::channel::<u64>(); // Worker: receives numbers, sends squares thread::spawn(move || { for n in jobs_rx { thread::sleep(Duration::from_millis(5)); results_tx.send(n * n).unwrap(); } }); // Send jobs for i in 1..=5 { jobs_tx.send(i).unwrap(); } drop(jobs_tx); // close the channel so the worker loop ends // Collect results via for loop (ends when all senders are dropped) let squares: Vec<u64> = results_rx.iter().collect(); println!("squares: {:?}", squares); // --- try_recv: non-blocking --- let (tx2, rx2) = mpsc::channel::<i32>(); tx2.send(42).unwrap(); match rx2.try_recv() { Ok(v) => println!("try_recv got: {v}"), Err(e) => println!("try_recv error: {e}"), } }
Result
Open