What does the future hold for IndexSet?

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?

1 Like

hash_map["my_key"] = 5i32 would actually behave like hash_map.insert("my_key", 5i32), if I remember correctly from that RFC discussion. The distinction of when the indexing syntax should behave like an insert vs a modification of an existing element was one of the concerns at the time.

The last try RFC 1129 got postponed because the impl would be insta-stable. The status of IndexSet is tracked by RFC issue 997.

(Can we not call the trait IndexSet? That sounds like something similar to HashSet and BTreeSet.)

I like RFC 1129's IndexAssign better.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.