Skip to content
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

Clarify behavior of HashSet::insert when there's existing key #107581

Closed
stepancheg opened this issue Feb 2, 2023 · 2 comments · Fixed by #107619
Closed

Clarify behavior of HashSet::insert when there's existing key #107581

stepancheg opened this issue Feb 2, 2023 · 2 comments · Fixed by #107619
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@stepancheg
Copy link
Contributor

Location

https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.insert

Summary

HashSet::insert is underspecified.

It says:

If the set already contained this value, false is returned.

But when the set already contains a value, is existing value replaced with new, or new value just discarded?

It does not make difference for primitive types, but can have significant effect when inserting Arc for example.

@stepancheg stepancheg added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Feb 2, 2023
@GuillaumeGomez
Copy link
Member

In case the entry already exists, it uses mem::replace() as you can see here (HashSet comes from the hashbrown crate and is in fact a HashMap without a value). But indeed, updating the docs to mention would be nice. Want to send a PR?

@cuviper
Copy link
Member

cuviper commented Feb 3, 2023

That mem::replace is only on the map value, which is () for a set. The key is not modified.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 14, 2023
Specify behavior of HashSet::insert

`HashSet::insert` does not replace the value with equal value.

Fixes rust-lang#107581.
@bors bors closed this as completed in e800d5a Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants