In Rust, you can get the number of key-value pairs in a HashMap using the len() method:
use std::collections::HashMap;
let mut scores: HashMap<&str, i32> = HashMap::new();
scores.insert("Alice", 95);
scores.insert("Bob", 87);
scores.insert("Charlie", 72);
let n = scores.len();
println!("{}", n); // 3
len() returns the number of entries. You can also check if it's empty with is_empty():
println!("{}", scores.is_empty()); // false
let empty: HashMap<&str, i32> = HashMap::new();
println!("{}", empty.len()); // 0
Assignment
Follow the Coding Tutorial and get the number of entries in scores!
Hint
Use scores.len() and assign it to count.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check