百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rust-phf

> 编程语言
开源

为 Rust 编译时生成静态映射

2.2K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

为 Rust 编译时生成静态映射

Rust-PHF

Documentation

Rust-PHF is a library to generate efficient lookup tables at compile time using perfect hash functions.

It currently uses the CHD algorithm by default and also ships an experimental ptrhash feature for an alternative MPHF layout.

MSRV (minimum supported rust version) is Rust 1.85.

Usage

PHF data structures can be constructed via either the procedural macros in the phf_macros crate or code generation supported by the phf_codegen crate.

To compile the phf crate with a dependency on libcore instead of libstd, enabling use in environments where libstd will not work, set default-features = false for the dependency:

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

phf_macros

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

To try the experimental alternative, enable ptrhash on both the generator and runtime crates you use:

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

Note

Currently, the macro syntax has some limitations and may not work as you want. See #196 for example. Integer literals in the first key's type shape must use explicit suffixes, such as 0u32, [0u8, 1], or (0u32, 1u32); 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.

phf_codegen

To use phf_codegen on build.rs, you have to add dependencies under [build-dependencies]:

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

When using the experimental ptrhash layout, enable the ptrhash feature on both phf_codegen and the runtime phf dependency so the generated constants match the runtime struct layout.

Then put code on build.rs:

…

and lib.rs:

#[derive(Clone)]
enum Keyword {
    Loop,
    Continue,
    Break,
    Fn,
    Extern,
}

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
    KEYWORDS.get(keyword).cloned()
}

pub fn parse_operator(operator: &str) -> Option<&'static str> {
    OPERATORS.get(operator).copied()
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rustrust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言