#hash-map #const #compile-time

nightly const_hashmap

Compile-time hash map for any const-hashable key

2 releases

Uses new Rust 2024

new 0.1.1 May 2, 2025
0.1.0 May 2, 2025

#19 in #compile-time

Download history 197/week @ 2025-04-28

197 downloads per month
Used in const_hashmap_macros

MIT/Apache

14KB
273 lines

const_hashmap

Constant HashMap, with support for anytype. nightly only.


lib.rs:

Const HashMap

A small, fixed-size hash map you can build at compile time and use in const contexts. Under the hood it stores an array of [Bucket<K, V>] of power-of-two size, uses FNV-1a hashing and linear probing, and requires K: ConstHash + ConstEq.

Unlike std::collections::HashMap, this map can be constructed and queried at compile time via const fn. It’s intended for compile-time lookup tables (e.g. embedded constants), not for dynamic resizing.

Feature flags

  • Nightly only: this crate depends on #![feature(const_trait_impl)] and other const fn nightly features.
  • No additional features in v0.1: everything is available in the core crate.

Work in progress: this is a proof-of-concept. API may change.

Quickstart

Add to your Cargo.toml:

[dependencies]
const_hashmap = "0.1.0"

In your crate root (or lib):

#![feature(const_trait_impl)]  // nightly only
use const_hashmap::{ConstMap, ConstHash, ConstEq, build_map, Bucket};

// 1. Implement ConstHash + ConstEq for your key type. &str is implemented from the get go


// 2. Build a small const map from a slice of (key, value) pairs.
//    `N` is a power of two ≥ 2×pairs.len().
const COLORS: ConstMap<&'static str, u8, 8> = build_map(&[
    ("red",   1),
    ("green", 2),
    ("blue",  3),
]);

// 3. Query at compile time or runtime.
const RED_ID: Option<&u8> = COLORS.get(&"red");
const MISSING: Option<&u8> = COLORS.get(&"purple");

fn main() {
    assert_eq!(RED_ID, Some(&1));
    assert_eq!(MISSING, None);
}

For more advanced keys (structs, enums), implement ConstHash and ConstEq

Dependencies

~200–630KB
~15K SLoC