Skip to content

Add a function to get the key from a HashMap #43143

Closed

Description

@iqualfragile

Basically just

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

sometimes you need a reference to a key with the lifetime of the collection.

Activity

  1. oli-obk commented on Jul 10, 2017

    @oli-obk
    Contributor

    If OccupiedEntry::key returned the reference with the proper lifetime, we could already do this in usercode.

  2. iqualfragile commented on Jul 10, 2017

    @iqualfragile
    Author

    @oli-obk the entry api would not work, because it requires &mut, this usecase would require the ability to have multiple references and therefore just &

  3. added
    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.
    and removed
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Nov 19, 2017
  4. dtolnay commented on Nov 19, 2017

    @dtolnay
    Member

    I don't love the name get_both but I agree that this is basic functionality that HashMap should expose. I would welcome a PR and we can refine the name separately.

  5. added a commit that references this issue on Mar 26, 2018
    ca521d3
  6. added a commit that references this issue on Mar 26, 2018
    d601e74
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

    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions