← All notes

HashSet: a set of unique hashed values

Answer: A hash set is implemented as a HashMap where the value is (). A HashSet requires that the elements implement the Eq and Hash traits. A HashSet is guaranteed to not have duplicate elements.

A hash map that only keeps keys

A hash set is implemented as a HashMap where the value is ().

As with HashMap, the elements have to implement the Eq and Hash traits.

#[derive(PartialEq, Eq, Hash)] is a frequent way to get those traits.

Equal keys are required to hash equally. Violating this property is a logic error.

The uniqueness contract

Rust by Example asks you to consider a HashSet as a HashMap where we just care about the keys, and it notes that HashSet<T> is a wrapper around HashMap<T, ()>.

The feature that matters is that a HashSet is guaranteed to not have duplicate elements.

Every set collection makes that promise; HashSet is one implementation, and BTreeSet is another.

If you insert a value that is already present, the new value will replace the old.

Set operations

Four primary set operations are listed, and each call returns an iterator.

union: get all the unique elements in both sets.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Rust standard library, HashSet; Rust by Example, HashSet. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Rust standard library, HashSet; Rust by Example, HashSet. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:1wmsd7bb

iinventedlight. 2026. "HashSet: a set of unique hashed values." iinventedlight Library, iil:note:1wmsd7bb, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-hashset/. License: CC-BY-4.0.

@misc{iil_note_1wmsd7bb,
  author = {{iinventedlight}},
  title = {{HashSet: a set of unique hashed values}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/rust-hashset/},
  note = {Permanent ID iil:note:1wmsd7bb. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-hashset.md.