That is to prevent bugs, for example,
Should &mut map[el]
make a new element if el
is not in the hashmap?
How about map[el] = value;
?
If your answers differed, then Rust can't implement IndexMut
for HashMap
because there is no way to distinguish between the two cases. Inserting on a mutable borrow is a footgun, because it differs from simply doing map.get_mut(el).unwrap()
, this difference would make it prone to bugs.