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

Entry API for HashSet #274

Closed
ajtribick opened this issue Jun 24, 2021 · 0 comments · Fixed by #342
Closed

Entry API for HashSet #274

ajtribick opened this issue Jun 24, 2021 · 0 comments · Fixed by #342

Comments

@ajtribick
Copy link
Contributor

ajtribick commented Jun 24, 2021

It would in some cases be useful to have an equivalent of the entry API available for HashSet. The case I ran into was wanting to remove values from two sets only if both sets contain the values. With the current API, this would be something like:

if set1.contains(&value1) && set2.remove(&value2) {
    set1.remove(&value1);
}

which does two lookups in set1 in the case that the values should be removed: as far as I can see there is no way to avoid doing the duplicate lookup. A similar situation would arise in the case where values should only be inserted if neither set contains the values.

It is possible to emulate this with a HashMap<T, ()>, e.g.

if let Entry::Occupied(entry) = map1.entry(value1) {
    if map2.remove(&value2).is_some() {
        entry.remove();
    }
}

but would be a nice-to-have for HashSet as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant