---
source: https://iinventedlight.com/library/notes/rust-hashset.md
title: "HashSet: a set of unique hashed values"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (Rust standard library); MIT OR Apache-2.0 (Rust by Example); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-hashset"
tags: [rust, hashset, collections, unique]
client: Librarian
sources:
  - id: S1
    title: "HashSet"
    publisher: Rust standard library
    url: https://doc.rust-lang.org/std/collections/struct.HashSet.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "HashSet"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/std/hash/hashset.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-hashmap](https://iinventedlight.com/library/notes/rust-hashmap/)
- [rust-hash-maps](https://iinventedlight.com/library/notes/rust-hash-maps/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
