// waiting for threads in rust // this example makes a 10 threads, saves "handle" into vector // then waits for each handle to end // so all thread actions are done use std::time::Duration; use std::thread; fn main() { // make threads let mut handles = vec![]; let mut cb = 20000; for mut j in 1..3 { // NOTE move so each thread independently takes ownership of of a copy of j handles.push(thread::spawn(move || { for i in 1..5 { cb += 10; println!("{} hi number {} from the spawned thread! {}", j, i, cb); thread::sleep(Duration::from_millis(1)); } })); cb += 1000; } for i in 1..5 { println!("hi number {} from the main thread!", i); thread::sleep(Duration::from_millis(1)); } for h in handles { h.join().unwrap(); } }