-
Notifications
You must be signed in to change notification settings - Fork 13k
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
and_modify
method should be part of the HashMap's Documentation.
#98122
Comments
To be clear, are you suggesting that the documentation for @rustbot label +A-docs +E-easy |
Yes, I am. Can I work on the PR for the issue? Thanks! |
@rustbot claim |
…ylan-DPC Entry and_modify doc This PR modifies the documentation for [HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html#) and [BTreeMap](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#) by introducing examples for `and_modify`. `and_modify` is a function that tends to give more idiomatic rust code when dealing with these data structures -- yet it lacked examples and was hidden away. This PR adds that and addresses rust-lang#98122. I've made some choices which I tried to explain in my commits. This is my first time contributing to rust, so hopefully, I made the right choices.
I would shorten this to:
IMO this is more idiomatic than the
In contrast with
Anyway with #98125 I suppose this could be closed? |
So honestly, I agree with what you are saying! However, if we look at the HashMap documentation right now we see this: // update a key, guarding against the key possibly not being set
let stat = player_stats.entry("attack").or_insert(100);
*stat += random_stat_buff(); I believe that |
I understand that
and_modify
deals with theEntry
enum; however, I believe the method would be advantageous to developers when dealing with HashMaps.A common pattern I see when using hashmaps is something like this:
However, with
and_modify
this turns into this:Which leans more into idiomatic rust. I just learned of
and_modify
today, and I believe more rust users would use it if they knew it existed. I think the problem is thatand_modify
is part of theEntry
API.The text was updated successfully, but these errors were encountered: