Chunks one vector with or without use rayon.
And then map some function to each part.
In the following example, the sum function was used.
use rayon::prelude::*;
fn main() {
let my_vec: Vec<_> = (0..25).collect();
let chunk_size = 10;
let chunks_without_rayon: Vec<_> = my_vec
.chunks(chunk_size)
.enumerate()
.map(|(i, chunk)| {
println!("chunk{i}: {chunk:?}");
chunk.iter().sum::<i32>()
})
.collect();
println!("chunks_without_rayon = {chunks_without_rayon:?}\n");
let chunks_with_rayon: Vec<_> = my_vec
.par_chunks(chunk_size)
.enumerate()
.map(|(i, chunk)| {
println!("chunk{i}: {chunk:?}");
chunk.iter().sum::<i32>()
})
.collect();
println!("chunks_with_rayon: {chunks_with_rayon:?}")
}
The output:
chunk0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
chunk1: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
chunk2: [20, 21, 22, 23, 24]
chunks_without_rayon = [45, 145, 110]
chunk0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
chunk1: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
chunk2: [20, 21, 22, 23, 24]
chunks_with_rayon: [45, 145, 110]