Implement fan-out/fan-in in Rust by spawning worker threads for each task (fan-out) and collecting their results via channels (fan-in).
use std::thread;
use std::sync::mpsc;
fn main() {
let (tx, rx) = mpsc::channel();
let mut handles = vec![];
// Fan-out: spawn workers
for i in 0..4 {
let tx = tx.clone();
handles.push(thread::spawn(move || {
tx.send(i * 2).unwrap();
}));
}
drop(tx);
// Fan-in: collect results
let results: Vec<i32> = rx.iter().collect();
for h in handles { h.join().unwrap(); }
println!("{:?}", results);
}