Fix the Iter
This code tries to print all animals sorted alphabetically and then print the total count. But it uses .into_iter() which moves the HashMap, so it can't be used afterward!
Fix the code so it prints:
text
cat: meow cow: moo dog: woof Total: 3
Hint: Change .into_iter() to .iter() so the HashMap is borrowed instead of moved. You'll also need to update the type annotation on sorted to use references.
rust
use std::collections::HashMap;
fn main() {
let mut animals = HashMap::new();
animals.insert("cat", "meow");
animals.insert("dog", "woof");
animals.insert("cow", "moo");
let mut sorted: Vec<(_, _)> = animals.into_iter().collect();
sorted.sort();
for (key, value) in sorted {
println!("{}: {}", key, value);
}
println!("Total: {}", animals.len());
}