std-type

HashMap<K, V> — key-value lookup

A map associates each distinct key with one value. The key must satisfy Eq and Hash for ordinary lookup and insertion; the hasher builder S defaults to RandomState. Keys and values are stored by ownership. Lookup has expected constant cost under suitable hashing, not a worst-case constant guarantee. Iteration order is unspecified. Expected stdout (not executed): 4 9 Common mistake: Assuming iteration follows insertion order; sort a collected sequence or choose BTreeMap for ordered keys.

Formal Syntax & Structure

rust syntaxrust
HashMap<K, V, S = RandomState>

Working Example

Example Snippetrust
fn main() {    use std::collections::HashMap;    let stock = HashMap::from([("pen", 4), ("paper", 9)]);    println!("{} {}", stock["pen"], stock["paper"]);}
Rust Reference