---
source: https://iinventedlight.com/library/notes/rust-hash-maps.md
title: "HashMap in Rust"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT); MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT); prose restates or closely paraphrases cited source lines"
license_family: rust-mit
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Storing Keys with Associated Values in Hash Maps - The Rust Programming Language, MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT); Rust by Example, HashMap - Rust By Example, MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: Rust"
tags: ['rust', 'hashmap', 'collections']
sources:
  - id: S1
    title: "Storing Keys with Associated Values in Hash Maps - The Rust Programming Language"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch08-03-hash-maps.html
    accessed: 2026-11-04
    license: "MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT)"
  - id: S2
    title: "HashMap - Rust By Example"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/std/hash.html
    accessed: 2026-11-04
    license: "MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT)"
---
# HashMap in Rust

**Answer:** The type HashMap<K, V> stores a mapping of keys of type K to values of type V using a hashing function, which determines how it places these keys and values into memory. Where vectors store values by an integer index, HashMaps store values by key. (S1, S2)

## Basics

- One way to create an empty hash map is to use new and to add elements with insert. (S1)
- If we insert references to values into the hash map, the values won't be moved into the hash map. (S1)
- If we insert a key and a value into a hash map and then insert that same key with a different value, the value associated with that key will be replaced. (S1)

## Keys and size

- HashMap keys can be booleans, integers, strings, or any other type that implements the Eq and Hash traits. (S2)
- Like vectors, HashMaps are growable, but HashMaps can also shrink themselves when they have excess space. (S2)

## Claims
- id: C1
  claim: HashMap maps K to V with hashing.
  source: S1
  evidence: "keys of type String and values of type i32."
- id: C2
  claim: Inserting the same key replaces the value.
  source: S1
  evidence: "the value associated with that key will be replaced."
- id: C3
  claim: Keys must implement Eq and Hash.
  source: S2
  evidence: "or any other type that implements the Eq and Hash traits."
- id: C4
  claim: HashMaps can shrink.
  source: S2
  evidence: "when they have excess space."

## Sources
- [S1] Storing Keys with Associated Values in Hash Maps - The Rust Programming Language. The Rust Programming Language. https://doc.rust-lang.org/book/ch08-03-hash-maps.html . License: MIT or Apache-2.0 (rust-lang/book; Copyright (c) 2010 The Rust Project Developers; https://github.com/rust-lang/book/blob/main/LICENSE-MIT). Accessed 2026-11-04.
- [S2] HashMap - Rust By Example. Rust by Example. https://doc.rust-lang.org/rust-by-example/std/hash.html . License: MIT or Apache-2.0 (rust-lang/rust-by-example; Copyright (c) 2014 Jorge Aparicio; https://github.com/rust-lang/rust-by-example/blob/master/LICENSE-MIT). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
