-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Expose a HashMap entry's key #1541
Comments
This seems pretty reasonable - I think the right way to move forward here would be to open a PR adding the methods (marked unstable of course). We'll want to cover both BTreeMap and HashMap for consistency. cc @rust-lang/libs. |
It's not too relevant, but I just thought I'd mention - a somewhat related case is if you have, say, a |
@comex, I was thinking along similar lines with an alternate form of Entry, but specialized for when the key type implements If this seems worth pursuing, does it make sense to get in a PR for the simpler case of modifying the current API first? |
Related: #1533 |
Expose the key of Entry variants for HashMap and BTreeMap. This PR addresses [issue 1541](rust-lang/rfcs#1541) by exposing the key of `HashMap` and `BTreeMap` entry variants. Basic tests are provided.
Point of order: when should this issue and #32281 in rust-lang/rust be closed? With the PR accepted, I'm not sure if it's my responsibility to close them. (Does that happen if/when the I'd like to follow up with additional discussion about how to improve the entry API. I'm currently considering how to expose #1533 is host to discussion about handling cloneable key types better. Is it time to try writing a more general RFC on the entry API (cf. Head-Desking on Entry API 4.0)? |
|
At present,
HashMap
'sOccupiedEntry
andVacantEntry
don't expose their associated keys. It would be useful to have a method (say,key()
) which exposes a reference to the key. The relevant use case I have run into is when performing a get-or-insert operation on aHashMap
, when the value that is inserted is a function of the key. If you want to compute the value to insert only when there is not already an entry in the map, the current API forces you to clone the key so that its value is still available after pulling anEntry
out of the map. For example:It appears simple to add methods which return a reference to the keys of
OccupiedEntry
andVacantEntry
. I would be happy to provide a pull request doing so.The text was updated successfully, but these errors were encountered: