diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 80b6def..4dd26c1 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -13,19 +13,22 @@ use std::thread; use std::time::{Duration, Instant}; +fn thread_func(i: u32) -> u128 { + let start = Instant::now(); + thread::sleep(Duration::from_millis(250)); + println!("thread {} is complete", i); + start.elapsed().as_millis() +} + fn main() { let mut handles = vec![]; for i in 0..10 { - handles.push(thread::spawn(move || { - let start = Instant::now(); - thread::sleep(Duration::from_millis(250)); - println!("thread {} is complete", i); - start.elapsed().as_millis() - })); + handles.push(thread::spawn(move || thread_func(i))); } let mut results: Vec = vec![]; for handle in handles { + results.push(handle.join().unwrap()); // TODO: a struct is returned from thread::spawn, can you use it? }