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.
I understand that
and_modifydeals with theEntryenum; 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_modifythis turns into this:Which leans more into idiomatic rust. I just learned of
and_modifytoday, and I believe more rust users would use it if they knew it existed. I think the problem is thatand_modifyis part of theEntryAPI.