Skip to main content

HashMap and the entry API

`HashMap<K, V>` stores values by key. `insert` takes ownership of both, `get` hands back an `Option<&V>`, and iteration order is deliberately unspecified — sort the keys when you need stable output.

The entry API is the piece worth learning early. `*map.entry(key).or_insert(0) += 1` looks up the key once and either inserts a default or hands you a mutable reference to what is already there.

It has to be imported: `use std::collections::HashMap;`. Only the true basics live in the prelude.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust