Processing vec in parallel: how to do safely, or without using unstable features?

Today the rayon crate is the de facto standard for this sort of thing:

use rayon::prelude::*;

fn main() {
    let mut data = vec![1, 2, 3];
    data.par_iter_mut()
        .enumerate()
        .for_each(|(i, x)| *x = 10 + i as u32);
    assert_eq!(vec![10, 11, 12], data);
}

Note that this is just one line different from the single-threaded version using standard iterators, which would replace par_iter_mut with iter_mut.

See also Writing a small ray tracer in Rust and Zig.

Leave a Comment