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
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.
License: CC-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.
Cite this
iinventedlight. 2026. "Rust HashMap: lookup by key." iinventedlight Library, iil:note:pkpskcfb, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-hashmap/. License: CC-BY-4.0.
@misc{iil_note_pkpskcfb,
author = {{iinventedlight}},
title = {{Rust HashMap: lookup by key}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-hashmap/},
note = {Permanent ID iil:note:pkpskcfb. 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-hashmap.schema.json (schema; CC-BY-4.0)
- rust-hashmap.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-hashmap.md.