Skip to main content

HashSet: membership and set maths

A `HashSet<T>` is a `HashMap` with no values — just keys. Inserting a duplicate returns `false` and changes nothing, which makes deduplication a one-liner.

Membership tests are constant time on average, so a set is the right answer whenever you find yourself scanning a `Vec` with `contains` inside a loop.

Unions, intersections and differences are built in and return iterators, so nothing is allocated until you `collect`.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust