Ryaon

Parllalel collection transformations using rayon

Mutating a vectors to double its elements can be done by,

let mut nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
// Transform sequentially
nums.iter_mut().for_each(|n| *n *= 2);
dbg!(nums);

With rayon it is easy to perform the same operation in parallel by using par iterators instead,

use rayon::prelude::*;

let mut nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
// Transform in parallel
nums.iter_mut().for_each(|n| *n *= 2);
dbg!(nums);

Last updated