Skip to content

and_modify method should be part of the HashMap's Documentation. #98122

Closed
@SeniorMars

Description

@SeniorMars

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 toolsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions