-
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 HashMap.entry_or_clone() method #1203
Comments
See also https://internals.rust-lang.org/t/head-desking-on-entry-api-4-0/2156 This is possibly a viable solution in that it's back-compat to add a new method that requires a Clone bound. |
Just hit this as well. The Asides from that, the name |
I just posted another alternative to this in #1533. |
Just passing by to give a +1, I also just ran into this and am annoyed. |
Eh... is that a Github bug? It says I unassigned Gankro, but I did no such thing nor do I have permissions to do so. |
Given that it has been 5 years and apparently nobody has found a more elegant solution perhaps it is time to accept that I'm really surprised this hasn't been given more attention given that it is a pretty basic flaw and Rust is very focused on performance. |
On nightly rust (or stable hashbrown) you can use the It needs another design pass though before it can be stabilized. It provides useful functionality, but the very clean initial design became a little mangled when it ran into reality. You can do what you want as some_map
.raw_entry_mut()
.from_key(&key)
.or_insert_with(|| (key.clone(), 42)); Note that unlike |
|
Sometimes I have a key type which implements Clone, but for efficiency reasons I don't want to clone it unless it's absolutely necessary.
In this case it would be nice to have a method
which behaves identically to
but only bothers to clone k if the key doesn't already exist in my_hash_map.
The text was updated successfully, but these errors were encountered: