I was quite surprised to find that while the Index trait was implemented for HashMap, IndexMut was not. You currently cannot do hash_map["my_key"] = 5i32
assuming that there is already a value associated to the key “my_key”. You have to do
if let Some(mut value) = hash_map.get_mut("my_key") {
*value = 5i32;
}
As someone answered on the rust IRC, the implementation of IndexMut been done to leave room to a potentially future trait IndexSet
, that would allow hash_map["my_key"] = 5i32
to actually insert an entry to that hash_map (instead of panicking). Implementing IndexMut now would either meaning letting IndexSet down of HashMap, or breaking backwards compatibility in the future.
An RFC talked about that long time ago, but the discussion around that trait has been sparse since, so my question is simply: what does the core team intend to do with the IndexSet trait? Is it something we can expect for the near future? Should we re-open an RFC to talk about this in further detail again?