-
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
Add hash_map::Entry.or_insert_with_key() method #1202
Comments
I would really rather just expose a See #1194 |
I don't think that works. e.get_key_and_value() |
@dnspies What you're trying to do is fundamentally unsafe - you can't put a value into a map which keeps a reference to its own key. You can use such a reference to construct the value, in which case @gankro's solution will work, because the borrow will expire after construction, and you can then call mutable methods on the entry again. |
That won't work with or_insert_with which clearly must be called with both borrows. Of course, I could avoid using that method and instead just match against the entry to see if it's vacant or not. In that case get_key() is sufficient. |
Add or_insert_with_key to Entry of HashMap/BTreeMap Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time. This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda. --- I'm fairly new to Rust, so any optimizations, corrections to types, better names, better documentation, or whatever else would be appreciated. I'd like to thank Arnavion on freenode for helping me to implement a very similar method when I found that `or_insert_with_key` was unavailable. As a somewhat-related note, this implements rust-lang/rfcs#1202 from 2015, so if this pull request is accepted, that should be closed.
Add or_insert_with_key to Entry of HashMap/BTreeMap Going along with `or_insert_with`, `or_insert_with_key` provides the `Entry`'s key to the lambda, avoiding the need to either clone the key or the need to reimplement this body of this method from scratch each time. This is useful when the initial value for a map entry is derived from the key. For example, the introductory Rust book has an example Cacher struct that takes an expensive-to-compute lambda and then can, given an argument to the lambda, produce either the cached result or execute the lambda. --- I'm fairly new to Rust, so any optimizations, corrections to types, better names, better documentation, or whatever else would be appreciated. I'd like to thank Arnavion on freenode for helping me to implement a very similar method when I found that `or_insert_with_key` was unavailable. As a somewhat-related note, this implements rust-lang/rfcs#1202 from 2015, so if this pull request is accepted, that should be closed.
As per rust-lang/rust#70996, we now have an unstable implementation of this in |
Stabilized in 1.50.0. See the method's documentation. |
In some cases, I only have one key, and by calling
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):
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
The text was updated successfully, but these errors were encountered: