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).
License: CC-BY-4.0 · 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).
Cite this
iinventedlight. 2026. "HashMap in Rust." iinventedlight Library, iil:note:3v8y8sjs, version of 2026-11-04. https://iinventedlight.com/library/notes/rust-hash-maps/. License: CC-BY-4.0.
@misc{iil_note_3v8y8sjs,
author = {{iinventedlight}},
title = {{HashMap in Rust}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/rust-hash-maps/},
note = {Permanent ID iil:note:3v8y8sjs. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/rust-hash-maps.md.