Here is what I am trying to do:
use std::collections::HashMap;
fn main() {
let mut my_map = HashMap::new();
my_map.insert("a", 1);
my_map.insert("b", 3);
my_map["a"] += 10;
// my expected outputs is my_map becomes {"b": 3, "a": 11}
}
Raises the following error:
error: cannot assign to immutable indexed content
--> src/main.rs:8:5
|
8 | my_map["a"] += 10;
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable
I don't really understand what that means, since I made the HashMap
mutable. When I try to update an element in a vector
, I get the expected result:
let mut my_vec = vec!{1,2,3};
my_vec[0] += 10;
println!{"{:?}", my_vec};
// [11, 2, 3]
What is different about HashMap
that I am getting the above error? Is there a way to update a value?