---
source: https://iinventedlight.com/library/notes/rust-hashmap.md
title: "Rust HashMap: lookup by key"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Programming Language); 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: The Rust Programming Language, Storing Keys with Associated Values in Hash Maps; Rust by Example, HashMap. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). 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-hashmap"
tags: [rust, hashmap, collections, systems]
client: Librarian
sources:
  - id: S1
    title: "Storing Keys with Associated Values in Hash Maps"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch08-03-hash-maps.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "HashMap"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/std/hash.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
---
# Rust HashMap: lookup by key
**Answer:** A HashMap stores values by key rather than by a numeric index. Create an empty map with HashMap::new and add entries with insert. Keys must implement Eq and Hash.

## Lookup by key

Look up data by a key that can be of any type, not by an index.

One way to start is HashMap::new, then insert to add elements.

HashMap is not in the prelude, so you first use it from the collections portion of the standard library.

## Keys and capacity

Vectors store values by an integer index. HashMaps store values by key instead.

A key may be a boolean, an integer, a string, or another type that implements the Eq and Hash traits.

HashMap::new() gives a default initial capacity, which the example book recommends.

## See also

- [rust-generics](https://iinventedlight.com/library/notes/rust-generics/)
- [rust-string-vs-str](https://iinventedlight.com/library/notes/rust-string-vs-str/)
- [rust-ownership-borrowing](https://iinventedlight.com/library/notes/rust-ownership-borrowing/)

## Claims

- id: C1
  claim: Hash maps look up data by a key that can be of any type, not by an index.
  source: S1
  evidence: "look up data not by using an index"

- id: C2
  claim: An empty hash map can be created with new and filled with insert.
  source: S1
  evidence: "use new and to add elements with insert"

- id: C3
  claim: HashMap is not in the prelude, so you need to use std::collections::HashMap.
  source: S1
  evidence: "use the HashMap from the collections portion"

- id: C4
  claim: Keys can be booleans, integers, strings, or other types that implement Eq and Hash.
  source: S2
  evidence: "implements the Eq and Hash traits"

## Sources

- [S1] Storing Keys with Associated Values in Hash Maps. The Rust Programming Language. https://doc.rust-lang.org/book/ch08-03-hash-maps.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.

- [S2] HashMap. Rust by Example. https://doc.rust-lang.org/rust-by-example/std/hash.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: The Rust Programming Language, Storing Keys with Associated Values in Hash Maps; Rust by Example, HashMap. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). 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).
