- 条件编译
- cfg_attr
- cfg!
条件编译
conditional-compilation.md
commit ccb1d87d6faa9ff528d22b96595a0e2cbb16c0f2
Rust 有一个特殊的属性,#[cfg],它允许你基于一个传递给编译器的标记编译代码。它有两种形式:
#[cfg(foo)]# fn foo() {}#[cfg(bar = "baz")]# fn bar() {}
它还有一些帮助选项:
#[cfg(any(unix, windows))]# fn foo() {}#[cfg(all(unix, target_pointer_width = "32"))]# fn bar() {}#[cfg(not(foo))]# fn not_foo() {}
这些选项可以任意嵌套:
#[cfg(any(not(unix), all(target_os="macos", target_arch = "powerpc")))]# fn foo() {}
至于如何启用和禁用这些开关,如果你使用 Cargo 的话,它们可以在你Cargo.toml中的[features]部分设置:
[features]# no features by defaultdefault = []# Add feature "foo" here, then you can use it.# Our "foo" feature depends on nothing else.foo = []
当你这么做的时候,Cargo传递给rustc一个标记:
--cfg feature="${feature_name}"
这些cfg标记集合会决定哪些功能被启用,并且因此,哪些代码会被编译。让我们看看这些代码:
#[cfg(feature = "foo")]mod foo {}
如果你用cargo build --features "foo"编译,他会向rustc传递--cfg feature="foo"标记,并且输出中将会包含mod foo。如果我们使用常规的cargo build编译,则不会传递额外的标记,因此,(输出)不会存在foo模块。
cfg_attr
你也可以通过一个基于cfg变量的cfg_attr来设置另一个属性:
#[cfg_attr(a, b)]# fn foo() {}
如果a通过cfg属性设置了的话这与#[b]相同,否则不起作用。
cfg!
cfg!语法扩展也让你可以在你的代码中使用这类标记:
if cfg!(target_os = "macos") || cfg!(target_os = "ios") {println!("Think Different!");}
这会在编译时被替换为一个true或false,依配置设定而定。
