簡體   English   中英

Rust 單標識符條件編譯(cfg)

[英]Rust conditional compilation(cfg) with single identifier

我試圖從條件編譯手冊@ doc.rust-lang.org中理解以下示例:

// This function is only included when either foo or bar is defined
#[cfg(any(foo, bar))]
fn needs_foo_or_bar() {
  // ...
}

那些foobar標識符代表什么?

這是target_os標識符的快捷方式還是它的用途?

條件編譯手冊上的語法定義來看, any(foo, bar)是一個ConfigurationPredicate (特別是ConfigurationAny變體),所以foo, bar是一個ConfigurationPredicateList ,因此foobar都是一個ConfigurationPredicate 因此,您可以使用它們有條件地為目標操作系統編譯。 或者你可以做一些這樣的自定義功能:

#[cfg(any(feature = "myfeature1", feature = "myfeature2"))]
pub struct Source {
    pub email: String,
    pub name: String,
}

有關自定義功能的更多信息,請參閱此問題

原來它與target_os ,它是您在構建/運行時在RUSTFLAGS中設置的:

RUSTFLAGS='--cfg foo'

帶有更高級示例的相關問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM