Ryaon

Parllalel collection transformations using rayon

Mutating a vectors to double its elements can be done by,
1
let mut nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
2
// Transform sequentially
3
nums.iter_mut().for_each(|n| *n *= 2);
4
dbg!(nums);
Copied!
With rayon it is easy to perform the same operation in parallel by using par iterators instead,
1
use rayon::prelude::*;
2
​
3
let mut nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
4
// Transform in parallel
5
nums.iter_mut().for_each(|n| *n *= 2);
6
dbg!(nums);
Copied!
Last modified 1yr ago
Copy link