【发布时间】:2021-08-01 06:48:35
【问题描述】:
我试图在两个条件下有条件地编译一段代码:
- 构建是否通过 Linux 完成
- 用户指定的功能标志“模拟”是否设置为真
我精简的Cargo.toml 具有这种通用结构
[package]
...
[features]
simulation = []
[dependencies]
crate1 = ...
crate2 = ...
[target.'cfg(target_os = "linux")'.dependencies]
crate3 = ...
crate4 = ...
有没有办法在我的 rust 代码中指定我希望在通过 Linux 完成构建并关闭“模拟”功能标志时编译一段代码,然后在构建时编译另一段代码是通过带有“模拟”功能标志的 Linux 完成的吗?比如:
#[cfg(feature = "simulation")] && #[cfg(target_os = "linux")] { println!("run some code here"); }
!#[cfg(feature = "simulation)] && #[cfg(target_os = "linux")] { println!("run some other code here"); }
【问题讨论】:
标签: linux rust rust-cargo