iter_mut returns an iterator of mutable references, letting you change collection items directly. In a loop, each item is accessed through a mutable reference, so *item updates the value. section 8.2
Updating every vector item
Given let mut vector2 = vec![10, 20, 30];, vector2.iter_mut().for_each(|x| *x += 100); changes the vector to [110, 120, 130]. The mut binding permits modification, and *x accesses the referenced value. section 8.2
fn main() {
let vector1 = vec![1, 2, 3];
let mut vector2 = vec![10, 20, 30];
for num in vector1.iter() { #1
println!("Printing a &i32: {num}");
}
for num in vector1 { #2
println!("Printing an i32: {num}");
}
for num in vector2.iter_mut() { #3
*num *= 10;
println!("num is now {num}");
}
println!("{vector2:?}");
// println!("{vector1:?}"); #4
}
Compare iter_mut with iter and value iteration in one runnable example; the output shows mutable updates producing [100, 200, 300]. section 8.2
