std-method

slice::chunks_mut

Mutate disjoint groups of a slice in sequence. The last group can be short. The method does not allocate a new collection. Expected stdout (not executed): [10, 2, 30, 4, 50] Common mistake: Treating the final group as if it always has two elements.

Formal Syntax & Structure

rust syntaxrust
slice.chunks_mut(chunk_size)

Working Example

Example Snippetrust
fn main() {    let mut values = [1, 2, 3, 4, 5];    for group in values.chunks_mut(2) { group[0] *= 10; }    println!("{values:?}");}
Rust Reference