Skip to content

HashMap should not take ownership of its keys #44271

Description

@Binero

Right now, to insert a new value into a std::collections::HashMap, one must transfer ownership of the key to the map. The map however does not seem to require ownership of the key.

The HashMap does not keep around a copy of the key however. It only stores its hash. This means that there is no way to get the key out of the HashMap again.

For example, take the signature of the insert method:

fn insert(&mut self, k: K, v: V) -> Option<V>

If the value is updated, it returns the old value, thus giving the user ownership of said value again. The key however, is lost forever. There is no way to get it back.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions