std-method
HashMap::iter_mut
Visit key-value pairs as (&K, &mut V), allowing updates to values without replacing keys. Iteration order is unspecified. Expected stdout (not executed): 8 18 Common mistake: Expecting mutable access to keys, which could invalidate hashing invariants.
Formal Syntax & Structure
rust syntaxrust
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>Working Example
Example Snippetrust
fn main() { use std::collections::HashMap; let mut prices = HashMap::from([("a", 10), ("b", 20)]); for (_, v) in prices.iter_mut() { *v -= 2; } println!("{} {}", prices["a"], prices["b"]);}