Imperative & procedural programming: statements, loops, mutation, procedures
#Rust supports imperative code fully, but mutation is opt-in: a binding is immutable unless declared let mut, and the borrow checker allows either one writer or many readers at a time. That rule removes the classic risk of procedural code, shared mutable state changed from two places at once. Iterator chains are equally idiomatic; a for loop is fine when it reads better.
Complete program
Complete program.
In practice: borrow inputs (&[T]) and return owned results; keep let mut to the smallest scope; use the entry API to update a map in one lookup.
HashMap iteration order is unspecified; BTreeMap keeps keys sorted, so the output is deterministic.
*entry(k).or_insert(0) += x dereferences the &mut u32 the entry returns; forgetting the * is a type error, not a silent bug.