#hash-map #const #constants #compile-time #build

map-to-const

轻松将 HashMap 转换为常量 [(K, V); N] 值

2 个不稳定版本

0.2.0 2022年3月19日
0.1.0 2022年3月18日

#2263 in 数据结构

MIT 许可证

14KB
123

map-to-const

github crates.io docs.rs build status

轻松将 HashMap<K, V> 转换为常量 [(K, V); N] 值。


此软件包与 Cargo 一起使用,需要类似于以下 Cargo.toml

[dependencies]
map-to-const = "0.2"

入门

在您的应用程序中添加一些用法。

以下是使用 map-to-const 的代码示例

use map_to_const::*;
use std::collections::HashMap;

fn main() {
    // Create a HashMap in some manner. Ideally, this will be formatted
    // and returned in an API response or similar.
    let my_map = HashMap::from([("testing", "123"), ("hello", "world")]);

    let const_value = map_to_const(&my_map, "my const name");

    println!("{const_value}");
    // prints:
    //   const MY_CONST_NAME: [(&str, &str); 2] = [
    //       ("hello", "world"),
    //       ("testing", "123"),
    //   ];
    
    // later in code, construct the hashmap from the `const` slice:
    // let my_map = HashMap::from(MY_CONST_NAME);
}

示例

您可以在 GitHub 项目仓库的 examples/ 文件夹中查看此软件包的示例用法。

贡献

欢迎贡献!打开一个 pull request 来修复错误,或者 打开一个问题 来讨论新功能或更改。

有关更多信息,请查看文档中的 贡献 部分。

许可证

本项目自豪地采用 MIT 许可证 (LICENSEhttp://opensource.org/licenses/MIT)。

map-to-const 可以根据 MIT 许可证进行分发。贡献将接受相同的许可证。

作者

无运行时依赖