这是我想要做的:
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;
// I expect my_map becomes {"b": 3, "a": 11}
}
Run Code Online (Sandbox Code Playgroud)
引发以下错误:
error[E0594]: cannot assign to immutable indexed content
--> src/main.rs:8:5
|
8 | my_map["a"] += 10;
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, i32>`
Run Code Online (Sandbox Code Playgroud)
我真的不明白这意味着什么,因为我做了HashMap可变的.当我尝试更新a中的元素时vector,我得到了预期的结果:
error[E0594]: cannot assign to data in …Run Code Online (Sandbox Code Playgroud)