-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Description
I understand that and_modify
deals with the Entry
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:
strings.into_iter().for_each(|string| {
let value = map.entry(string).or_insert(0);
*value += 1;
});
However, with and_modify
this turns into this:
strings.into_iter().for_each(|string| {
map.entry(string)
.and_modify(|value| *value += 1)
.or_insert(1);
});
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 that and_modify
is part of the Entry
API.
Metadata
Metadata
Assignees
Labels
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.