std-method
slice::iter_mut — edit elements in place
The iterator yields mutable references to distinct elements, allowing an in-place update. Do not resize a borrowed Vec during this traversal. Expected stdout (not executed): [3, 5, 7] Common mistake: Trying to push into the vector during its mutable iteration.
Formal Syntax & Structure
rust syntaxrust
fn iter_mut(&mut self) -> std::slice::IterMut<'_, T>Working Example
Example Snippetrust
fn main() { let mut quantities = vec![2, 4, 6]; for quantity in quantities.iter_mut() { *quantity += 1; } println!("{quantities:?}");}