Rust hashmap interface remove and insert

Hi, I'm new to rust but have an extensive background in C/C++.

I was wondering why in std::collections::HashMap the insert method takes the key argument by value and in the remove method the key argument is a reference?

I've been reading the excellent "Rust for Rustaceans" (Borrowed vs Owned) and the implication there is that where a value is owned in a class it should be passed by value.

I would have thought though that this then makes the interface inconsistent as in this case. Since calling remove requires a reference operator for value types.

insert needs to store the key in the map, hence it needs ownership of it. remove instead only needs to determine which entry to remove, so it doesn't need ownership.

4 Likes