macro_rules! env { ($name:expr $(,)?) => { ... }; ($name:expr, $error_msg:expr $(,)?) => { ... }; }
Expand description
在编译时检查环境变量。
该宏将在编译时扩展为指定的环境变量的值,从而产生 &'static str 类型的表达式。
如果要在运行时读取值,请改用 std::env::var。
如果未定义环境变量,则将发出编译错误。
为了不产生编译错误,请改用 option_env! 宏。
Examples
let path: &'static str = env!("PATH");
println!("the $PATH variable at the time of compiling was: {path}");Run您可以通过将字符串作为第二个参数传递来自定义错误消息:
如果未定义 documentation 环境变量,则会出现以下错误:
error: what's that?!