Split a vector into smaller blocks (functional way)

I would like to split a vector into smaller blocks of n elements. For example:

let items: Vec<i32> = (0..10).map(|x| x).collect();
let mut iter = items.chunks(3);

let mut output: Vec<&[i32]> = vec![];
while let Some(x) = iter.next() {
    output.push(x);
}
println!("{:?}", output);
// prints: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

but I would like to do it in a "functional way". Something like this:

let output: Vec<&[i32]> = items.iter().some().misterious().functions().collect();

Is that possible?

Oh my God!

let output: Vec<&[i32]> = items.chunks(3).collect();
println!("{:?}", output);

Sorry.

1 Like