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
- id: C1 claim: HashSet is implemented as a HashMap whose value is (). source: S1 evidence: "implemented as a HashMap where the value is ()"
- id: C2 claim: Elements must implement Eq and Hash. source: S1 evidence: "elements implement the Eq and Hash traits"
- id: C3 claim: Equal keys must have equal hashes. source: S1 evidence: "if two keys are equal, their hashes must be equal"
- id: C4 claim: A HashSet is guaranteed not to have duplicate elements. source: S2 evidence: "guaranteed to not have duplicate elements"
- id: C5 claim: Inserting a present value replaces the old one. source: S2 evidence: "the new value will replace the old"
Sources
- [S1] HashSet. Rust standard library. https://doc.rust-lang.org/std/collections/struct.HashSet.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] HashSet. Rust by Example. https://doc.rust-lang.org/rust-by-example/std/hash/hashset.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
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
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.},
}
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.
- rust-hashset.schema.json (schema; CC-BY-4.0)
- rust-hashset.table.json (table; CC-BY-4.0)
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.