Why does HashMap.entry take a K?


#1

HashMap.entry takes a K:
fn entry(&mut self, key: K) -> Entry<K, V>
Whereas many other HashMap accessors such as get, contains_key, etc, take:
fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where K: Borrow<Q>, Q: Hash + Eq
If you use entry, and the map happenned to already contain that key, and you need to use the same key again later, now you’ve made a useless copy. IMO, better would be:
fn entry<Q: ?Sized>(&mut self, k: &Q) -> Entry<K, V> where K: Borrow<Q>, for<'q> &'q Q: Into<K>, Q: Hash + Eq


#2

Everything is explained here: https://internals.rust-lang.org/t/head-desking-on-entry-api-4-0/2156