use std::thread;
use std::sync::mpsc;
fn main() {
// Create a channel for communication between threads
let (sender, receiver) = mpsc::channel();
// Spawn multiple threads
for i in 0..5 {
let sender_clone = sender.clone();
thread::spawn(move || {
// Simulate some work in each thread
let result = i * 2;
// Send the result back through the channel
sender_clone.send(result).expect("Failed to send result");
});
}
// Gather results from threads
for _ in 0..5 {
match receiver.recv() {
Ok(result) => {
// Process the result
println!("Received result: {}", result);
}
Err(err) => {
eprintln!("Error receiving result: {}", err);
}
}
}
}