英文:
Rust use different variable value for Cargo profile
问题
这在文档中出奇地难以找到。我想要为每个 Cargo 配置文件使用不同的 base_url 作为我的 API 终点。因此,--profile=debug 将使用 https://localhost:3000,而 --profile=release 将使用 https://api.cooldomain.io/ 或类似的内容。有什么指导意见吗?
英文:
This is surprisingly hard to find in the documentation. I want to use a different base_url for my API endpoint for every Cargo profile. So, —-profile=debug will use https://localhost:3000, and —-profile=release will use https://api.cooldomain.io/ or something like that.
Any pointers?
答案1
得分: 3
你可以使用#[cfg(debug_assertions)]来为调试配置设置你的base_url,使用#[cfg(not(debug_assertions))]来为非调试配置设置。
例如像这样:
#[cfg(debug_assertions)]
const BASE_URL: &'static str = "http://localhost:3000";
#[cfg(not(debug_assertions))]
const BASE_URL: &'static str = "https://api.cooldomain.io/";
fn main() {
println!("{}", BASE_URL);
}
你可以在这里阅读更多关于debug_assertions的信息:这里
英文:
You can set your base_url using #[cfg(debug_assertions)] for debug profiles and using #[cfg(not(debug_assertions))] for non debug profiles.
For example like this
#[cfg(debug_assertions)]
const BASE_URL:&'static str = "http://localhost:3000";
#[cfg(not(debug_assertions))]
const BASE_URL:&'static str = "https://api.cooldomain.io/";
fn main() {
println!("{}",BASE_URL);
}
You can read more about debug_assertions here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论