indexmap provides IndexMap and IndexSet structures that match std HashMap ergonomics while preserving insertion order and allowing index-based access. Lookup works by key or by position in the sequence.
Overview
indexmap is a pure-Rust hash table crate whose iteration order does not depend on hash values. Keys stay in insertion order until operations such as remove or swap_remove intentionally change ordering; shift_remove keeps relative order among remaining entries.
Internally the design combines a hash index table with a dense vector of key-value pairs, which makes full iteration fast and keeps memory layout compact. The crate builds on hashbrown, the same table technology used by Rust’s standard HashMap.
IndexMap and IndexSet aim to be drop-in alternatives to std collections while adding sorting helpers, pop methods, flexible key equivalence via Equivalent, and optional mutable access traits for keys and set values. Feature flags gate serde, rayon parallelism, quickcheck, and no_std builds that rely on alloc.
The project notes inspiration from Python 3.6 dict ordering semantics, adapted to Rust’s ownership model. It has seen use in large Rust codebases such as the compiler when hash map behavior and iteration cost matter.
Key features
- Insertion-ordered map and set types
- Key lookup and index-based access
- hashbrown-backed hash tables
- shift_remove preserves order
- Sorting and pop APIs
- serde and rayon optional features
- no_std support without default std flag
- Custom hasher type aliases supported
Use cases
- Deterministic iteration in Rust services
- Configs needing stable key ordering
- Sets/maps compatible with std APIs
- serde serialization of ordered maps