是否可以在编译时从环境变量初始化变量? [英] Is it possible to initialize a variable from an environment variable at compilation time?

查看:48
本文介绍了是否可以在编译时从环境变量初始化变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译期间初始化一个变量.比如我想在编译代码时将变量VAR初始化为VALUE:

I would like to initialize a variable during compilation time. For example, I would like to initialize the variable VAR to VALUE when compiling the code:

match env::var("VAR") {
    Ok(value) => println!("Ok {}", value),
    Err(e) => println!("Error ({})", e),
};

但是,我想在 no_std 上下文中执行此操作,因此,我无法使用 std::env 访问环境.可以这样做吗?

However, I wanted to do it in a no_std context, therefore, I cannot use std::env to access the environment. Is it possible to do this?

推荐答案

env::var 在编译时不会得到评估:

从当前进程中获取环境变量键.

Fetches the environment variable key from the current process.

您正在寻找 std::env!/core::env!std::option_env!/core::option_env!.

You are looking for std::env! / core::env! or std::option_env! / core::option_env!.

这篇关于是否可以在编译时从环境变量初始化变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆