1、示例:對不同的操作系統調用不同的代碼。
use libc;
#[cfg(target_os="linux")]
fn my_print(){
unsafe{
libc::printf("hello".as_ptr() as *const libc::c_char);
}
}
#[cfg(target_os="windows")]
fn my_print(){
unsafe{
libc::puts("hello".as_ptr() as *const libc::c_char);
}
}
fn main() {
my_print();
}
2、cfg宏
if cfg!(target_os="windows"){
println!("windows");
}
else{
println!("not windows")
}
if與else代碼分支的都會編譯。以下代碼無法通過編譯,因為在windows下生成時,libc中沒有printf函數:
if cfg!(target_os="windows"){
libc::printf("hello".as_ptr() as *const libc::c_char);
}
else{
libc::puts("hello".as_ptr() as *const libc::c_char);
}
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 default default = [] # The “secure-password” feature depends on the bcrypt package. secure-password = ["bcrypt"]
當你這么做的時候,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!
[語法擴展](Compiler Plugins 編譯器插件.md)也讓你可以在你的代碼中使用這類標記:
if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
println!("Think Different!");
}
這會在編譯時被替換為一個true
或false
,依配置設定而定。