Library / SDK
rust-phf/rust-phf avatar
rust-phf/rust-phf

rust-phf builds perfect hash maps at compile time, and the macro only accepts a narrow set of key shapes

Compile time static maps for Rust

2,225 stars142 forksRustMIT

At a glance

What is it?
rust-phf/rust-phf is a Rust workspace that generates lookup tables with perfect hash functions, either through the phf_map! macro or from build.rs. The macro is the shorter path and the code generator is the one without syntax limits.
Who is it for?
Use phf when a Rust program has a fixed set of string or small tuple keys known at build time, such as keywords or operators, and you want a static map with no startup cost. Skip it for tables that change at runtime.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly Rust, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 26, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • rust-phf/rust-phf2,225 ★ · +34
Star history
Repository2026-10-082026-11-26
rust-phf/rust-phf2.2k2.2k

34 Stars in the last 49 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

phf_map! builds a static map from a macro invocation

The short route to a compile-time map is the procedural macro in the phf_macros crate, switched on through a feature of the phf crate:

toml
[dependencies]
phf = { version = "0.14.0", features = ["macros"] }

The README example maps keyword strings to enum variants. It declares a Keyword enum with Loop, Continue, Break, Fn and Extern, then a static KEYWORDS of type phf::Map<&'static str, Keyword> filled by phf_map! with entries like "loop" => Keyword::Loop.

The result is a static item, so there is no initialization at program start and no lazy cell to guard. A lookup is a method call on the map. The tradeoff is that the contents are frozen when the crate compiles. A table that gains entries from a config file or user input needs an ordinary HashMap instead.

OR patterns let several keys share one value

The macro accepts OR patterns with the pipe character, so multiple keys can point to the same value without repeating the entry. The README shows a map of operators where "+" | "add" | "plus" => "addition" and "-" | "sub" | "minus" => "subtraction", and the multiplication line follows the same shape with "*", "mul" and "times".

This helps with alias tables, such as short and long spellings of a command. The pattern can also nest inside key expressions, as in ('p', "Point2d" | "Point3d") or [0u8 | 1, 2 | 3], where one position in a compound key accepts alternatives.

The benefit is a compact source table. The lookup side stays the same: the map holds one entry per accepted key, and a call such as OPERATORS.get(operator) returns the shared value for any of the aliases.

The macro limits are documented, and #196 is the example

A note in the README admits that the macro syntax has some limitations and may not work as you want, pointing at issue #196. Four rules follow. Integer literals in the first key's type shape must use explicit suffixes, such as 0u32, [0u8, 1] or (0u32, 1u32), while later keys infer unsuffixed integer literals from the same position in that first key. Tuple keys are supported up to 12 elements. All keys must use the same supported key expression type as the first key.

The practical failure is a compile error on a key list that looks reasonable. A table that mixes a string key with a tuple key will not compile, and a map keyed by unsuffixed integers needs the first key suffixed. A 13-element tuple key is out.

When a table does not fit these rules, the escape is phf_codegen, which builds the map from ordinary Rust code in build.rs and so has no macro grammar to satisfy.

phf_codegen moves the work into build.rs

The second construction path generates source text during the build. The dependencies sit in two places: phf without default features as a build dependency, and phf_codegen next to it.

toml
[build-dependencies]
phf = { version = "0.14.0", default-features = false }
phf_codegen = "0.14.0"

The build script writes a file named codegen.rs into the directory from the OUT_DIR environment variable. It builds a phf_codegen::Map with .entry calls, such as .entry("loop", "Keyword::Loop"), and writes the result as a static item. The library then pulls it in with include!(concat!(env!("OUT_DIR"), "/codegen.rs")), and exposes functions like parse_keyword that call KEYWORDS.get(keyword).cloned().

Values are passed as strings of Rust source, so the generator does not check them. A typo in "Keyword::Loop" shows up as a compile error in the generated file, not in build.rs. This path costs a build script and an include, and in return it accepts keys and values assembled by any Rust code, for instance read from a data file.

no_std works by switching off default features

The phf crate can compile against libcore instead of libstd, which allows use where libstd does not work. The README shows the line for that:

toml
[dependencies]
# to use `phf` in `no_std` environments
phf = { version = "0.14.0", default-features = false }

This is also the form used for the build dependency above, since a build script gains nothing from the standard library features. The macros feature is separate, so a no_std crate that wants phf_map! enables macros while keeping default features off.

The README documents none of what the default features contain, so check the phf crate's manifest on docs.rs before relying on a specific item in a no_std build. The minimum supported Rust version is 1.85, which is the floor for every crate in the workspace that follows the README.

ptrhash is an experimental layout with spare slots

By default phf uses the CHD algorithm. An experimental ptrhash feature offers an alternative layout that keeps spare slots, about 1% for large tables, to avoid remapping lookups. Iteration skips those slots, so each entry is returned exactly once.

Enabling it takes care on both sides:

toml
[dependencies]
phf = { version = "0.14.0", features = ["macros", "ptrhash"] }

For code generation the feature goes on phf_codegen and the runtime phf dependency together, so the generated constants match the runtime struct layout. Turning it on in only one crate produces constants that disagree with the struct they fill.

The README labels ptrhash experimental and gives no numbers comparing it with CHD, so there is no measured reason in the repository to switch. Treat it as something to test on your own table and keep CHD for anything you cannot afford to revisit.

A workspace of small crates, active in October 2026

The Cargo workspace lists phf, phf_codegen, phf_generator, phf_macros, phf_shared, test crates for the macros and codegen, and two examples named unicase-example and uncased-example under phf/examples. A patch section points crates.io versions of phf, phf_generator, phf_macros and phf_shared at the local paths.

The repository is not archived. The last push was 2026-10-24, and v0.14.0 was released on 2026-06-21, following v0.13.1 and v0.13.0, both released on 2025-08-23. The license is MIT. A RELEASE_PROCESS.md and a deny.toml sit at the top level, which cover releasing and dependency policy.

The versions in the README examples are 0.14.0, so copy them from there. A project on the earlier 0.13 line will see changes when it moves up, and the changelog is the place to read them.

Editorial conclusion

Use phf when a Rust program has a fixed set of string or small tuple keys known at build time, such as keywords or operators, and you want a static map with no startup cost. Skip it for tables that change at runtime. Before adopting the macro, write your real key list into phf_map! and see whether the key-type rules accept it, or move to phf_codegen in build.rs.

Frequently asked questions

Is Rust considered OOP, and does rust-phf need it?

The repository does not discuss that. Its README shows plain enums and statics, such as a Keyword enum used as map values in phf_map!.

What is the Rust language used for in rust-phf?

Here Rust is used to generate efficient lookup tables at compile time with perfect hash functions. The library targets cases such as keyword and operator tables.

What is Rust Linux, and is it related to rust-phf?

The repository does not mention Linux. The crates support no_std use through default-features = false, and the minimum supported Rust version is 1.85.

Official sources

  1. Issues
  2. License: MIT
  3. README
  4. Releases
  5. rust-phf/rust-phf on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/rust-phf-rust-phf.svg)](https://hysenlabs.com/projects/rust-phf-rust-phf)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/rust-phf-rust-phf.svg)](https://hysenlabs.com/projects/rust-phf-rust-phf)

Was this useful?