- rayon
- par_iter
- map
replace map with par_iter
examples/rayon/par-iter/src/main.rs
use rayon::prelude::*; fn main() { let numbers = (1..10).collect::<Vec<_>>(); let linear = numbers.iter().map(|number| number * 2).collect::<Vec<_>>(); let parallel = numbers .par_iter() .map(|number| number * 2) .collect::<Vec<_>>(); println!("{numbers:?}"); println!("{linear:?}"); println!("{parallel:?}"); assert_eq!(linear, parallel); let _parallel = numbers .par_iter() .map(|number| { println!("{:?}", std::thread::current().id()); number * 2 }) .collect::<Vec<_>>(); }
[1, 2, 3, 4, 5, 6, 7, 8, 9] [2, 4, 6, 8, 10, 12, 14, 16, 18] [2, 4, 6, 8, 10, 12, 14, 16, 18] ThreadId(8) ThreadId(9) ThreadId(2) ThreadId(17) ThreadId(6) ThreadId(5) ThreadId(7) ThreadId(3) ThreadId(15)