3 [![CI](https://github.com/rust-phf/rust-phf/actions/workflows/ci.yml/badge.svg)](https://github.com/rust-phf/rust-phf/actions/workflows/ci.yml) [![Latest Version](https://img.shields.io/crates/v/phf.svg)](https://crates.io/crates/phf)
5 [Documentation](https://docs.rs/phf)
7 Rust-PHF is a library to generate efficient lookup tables at compile time using
8 [perfect hash functions](http://en.wikipedia.org/wiki/Perfect_hash_function).
11 [CHD algorithm](http://cmph.sourceforge.net/papers/esa09.pdf) and can generate
12 a 100,000 entry map in roughly .4 seconds.
14 MSRV (minimum supported rust version) is Rust 1.60.
18 PHF data structures can be constructed via either the procedural
19 macros in the `phf_macros` crate or code generation supported by the
22 To compile the `phf` crate with a dependency on
23 libcore instead of libstd, enabling use in environments where libstd
24 will not work, set `default-features = false` for the dependency:
28 # to use `phf` in `no_std` environments
29 phf = { version = "0.11", default-features = false }
46 static KEYWORDS: phf::Map<&'static str, Keyword> = phf_map! {
47 "loop" => Keyword::Loop,
48 "continue" => Keyword::Continue,
49 "break" => Keyword::Break,
51 "extern" => Keyword::Extern,
54 pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
55 KEYWORDS.get(keyword).cloned()
61 phf = { version = "0.11", features = ["macros"] }
66 Currently, the macro syntax has some limitations and may not
67 work as you want. See [#183] or [#196] for example.
69 [#183]: https://github.com/rust-phf/rust-phf/issues/183
70 [#196]: https://github.com/rust-phf/rust-phf/issues/196
74 To use `phf_codegen` on build.rs, you have to add dependencies under `[build-dependencies]`:
78 phf = { version = "0.11.1", default-features = false }
79 phf_codegen = "0.11.1"
82 Then put code on build.rs:
87 use std::io::{BufWriter, Write};
91 let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
92 let mut file = BufWriter::new(File::create(&path).unwrap());
96 "static KEYWORDS: phf::Map<&'static str, Keyword> = {}",
97 phf_codegen::Map::new()
98 .entry("loop", "Keyword::Loop")
99 .entry("continue", "Keyword::Continue")
100 .entry("break", "Keyword::Break")
101 .entry("fn", "Keyword::Fn")
102 .entry("extern", "Keyword::Extern")
106 write!(&mut file, ";\n").unwrap();
122 include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
124 pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
125 KEYWORDS.get(keyword).cloned()