Skip to content

Instantly share code, notes, and snippets.

@kriogenia
Last active August 11, 2022 18:25
Show Gist options
  • Save kriogenia/795eefe64b2d92e9c33ee8830e7babdb to your computer and use it in GitHub Desktop.
Save kriogenia/795eefe64b2d92e9c33ee8830e7babdb to your computer and use it in GitHub Desktop.
Before scoped threads
use std::collections::HashMap;
fn main() -> Result<(), String> {
let numbers = vec![1, 2, 3, 4, 5, 6];
// This thread calculates average
let median_thread =
std::thread::spawn(|| numbers.iter().sum::<i32>() as f32 / numbers.len() as f32);
// This thread calculates mode
let mode_thread = std::thread::spawn(|| {
let mut counter = HashMap::new();
for value in numbers.iter() {
*counter.entry(value).or_insert(0) += 1;
}
counter.into_iter()
.max_by_key(|&(_, count)| count)
.map(|(val, _)| val)
});
// We join both threads and get the results if successful
let (median, mode) = match (median_thread.join(), mode_thread.join()) {
(Ok(median), Ok(Some(mode)) )=> Ok((median, mode)),
_ => Err("Calculations have failed".to_string())
}?;
println!("For the vector {numbers:?} the median is {median} and the mode is {mode}");
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment