Skip to content

Add HashMap::get_pair? #27237

Closed
Closed
@SimonSapin

Description

@SimonSapin

HashMap has this method:

    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
        where K: Borrow<Q>, Q: Hash + Eq
    {
        self.search(k).map(|bucket| bucket.into_refs().1)
    }

I’d like to add this one, where the only difference is removing .1:

    pub fn get_pair<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
        where K: Borrow<Q>, Q: Hash + Eq
    {
        self.search(k).map(|bucket| bucket.into_refs())
    }

This can be useful when K is &'a T where 'a is a longer lifetime than in Q.

Is this too obscure to pull its weight in std?

CC @gankro

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