NaaarK (@Qi_Nark)利用 Cargo build-scripts 将变量在编译期传递给运行时 中发帖

背景
ESP32 的 Rust 教程示例项目中,第一个项目 hardware-check 演示了如何利用开发板自带的 WIFI 连接无线网。像 WIFI SSID 与密码 这样的变量当然不应该直接写进代码,示例中利用了 toml-cfg#toml_config 宏,读取 cfg.toml 中的值并注入代码(cfg.toml git ignore),以做到在运行时使用这些变量。

这里示例代码稍有误导:在 build.rs 中声明 struct 并调用了一次 #[toml_cfg::toml_config],随即对获取到的值做了一些简单检查。然后在 main.rs 中用完全一样的方法再次声明 struct 并调用宏,并在后续连接 WIFI 时使用。在 main.rs 中还加了一段注释:/// This configuration is picked up at compile time b...