Skip to content

Add hash_map::Entry.or_insert_with_key() method #1202

Closed
@dnspies

Description

@dnspies

In some cases, I only have one key, and by calling

let e = my_hash_map.entry(k);

I'm giving up my one and only instance of k.
Now I need to use k to populate the entry (imagine my_hash_map memoizes the function my_fun):

e.or_insert_with(|| my_fun(&k))

but I'm out of luck because k has been stolen by the call to entry (k has been moved). By adding an extra function to entry, this can be rectified by

e.or_insert_with_key(|kref| my_fun(kref))

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions