Avatar ·

How to update a value in a mutable HashMap in Rust?

Hi! I just started learning Rust and encountered a problem when working with `HashMap`. I have a mutable hash table that stores "username" -> "score" pairs. I want to update the value for an existing key, for example, increase the score by 10. Here is my code: ```rust use std::collections::HashMap; fn main() { let mut scores = HashMap::new(); scores.insert("Alice".to_string(), 50); scores.insert("Bob".to_string(), 30); // Trying to update Alice's score let key = "Alice".to_string(); scores[key] += 10; // Error! println!("{:?}", scores); } ``` The compiler gives an error: `cannot index into a value of type HashMap`. I understand that `HashMap` does not implement the `IndexMut` trait for mutable access, but how then do I