std-method

array::each_mut

Obtain a fixed-length array of exclusive references to distinct elements. Each element can be updated through its corresponding reference. Expected stdout (not executed): [11, 22] Common mistake: Trying to access the original array while those mutable references are still needed.

Formal Syntax & Structure

rust syntaxrust
array.each_mut() -> [&mut T; N]

Working Example

Example Snippetrust
fn main() {    let mut scores = [10, 20];    let [a, b] = scores.each_mut();    *a += 1;    *b += 2;    println!("{scores:?}");}
Rust Reference