medium.com 6 min read Original Source

I Used HashMap for Years Without Knowing What Happens During a Resize

Part of the Data Structures in Rust series.

The linked list fought the borrow checker. The binary tree worked with it naturally. The hash map does something more interesting than either: it reveals why the standard library’s HashMap API is shaped the way it is, and once you have built one yourself, the design decisions that previously looked arbitrary start looking inevitable.

Most engineers use HashMap daily without thinking about what is underneath. You insert a key, you retrieve a value, and the whole thing is fast enough that you never have to care about the implementation. Building one from scratch does not make you faster at using the standard library version. It makes you stop being surprised by the parts of the API that initially seemed strange: why get returns Option<&V>, why you cannot hold a reference to a value while inserting a new key into the same map, and what the Entry API is actually solving.

What a hash map actually is

A hash map is an array of buckets. You take a key, run it through a hash function to get an index…