-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 or_insert_with_key to Entry of HashMap/BTreeMap #70996
Conversation
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.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @LukasKalbertodt (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Could you also submit a PR to hashbrown? I would like to keep the APIs in sync.
@Amanieu I've made a pull request to |
Add or_insert_with_key to Entry of HashMap 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. --- This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
@bors r+ rollup |
📌 Commit db0c39f has been approved by |
Add or_insert_with_key to Entry of HashMap 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. --- This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
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 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. --- This is modified from rust-lang/rust#70996 and intended to sync the APIs of `std` and `hashbrown`.
Rollup of 5 pull requests Successful merges: - rust-lang#70644 (Clean up `ModuleConfig` initialization) - rust-lang#70937 (Fix staticlib name for *-pc-windows-gnu targets) - rust-lang#70996 (Add or_insert_with_key to Entry of HashMap/BTreeMap) - rust-lang#71020 (Store UNICODE_VERSION as a tuple) - rust-lang#71021 (Use write!-style syntax for MIR assert terminator) Failed merges: r? @ghost
Going along with
or_insert_with
,or_insert_with_key
provides theEntry
'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.