Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iqualfragile opened this issue Jul 10, 2017 · 3 comments
Closed

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

iqualfragile opened this issue Jul 10, 2017 · 3 comments
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@iqualfragile
Copy link

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.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 10, 2017

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

@iqualfragile
Copy link
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 &

@Mark-Simulacrum Mark-Simulacrum added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jul 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 28, 2017
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 19, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 19, 2017

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.

kennytm added a commit to kennytm/rust that referenced this issue Mar 26, 2018
…mulacrum

Implement get_key_value for HashMap, BTreeMap

Fixes rust-lang#43143

Follow up from rust-lang#46992
TimNN added a commit to TimNN/rust that referenced this issue Mar 26, 2018
…mulacrum

Implement get_key_value for HashMap, BTreeMap

Fixes rust-lang#43143

Follow up from rust-lang#46992
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants