99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

條件編譯

2018-08-12 22:03 更新

條件編譯

Rust 有一個特殊屬性 #[cfg],它允許你編譯基于標志的代碼并傳遞給編譯器。它有兩種形式:

    #[cfg(foo)]

    #[cfg(bar = "baz")]

他們也有一些幫助:

    #[cfg(any(unix, windows))]

    #[cfg(all(unix, target_pointer_width = "32"))]

    #[cfg(not(foo))]

這些可以隨意嵌套:

    #[cfg(any(not(unix), all(target_os="macos", target_arch = "powerpc")))]

至于如何啟用或禁用這些開關,如果你使用 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" 編譯代碼,它將發(fā)送 --cfg feature="foo" 標識給 rustc,且輸出中包含 mod foo。如果我們定期地使用 cargo build 編譯它,也不傳遞額外的標識,就不會存在任何 foo 模塊。

cfg_attr

你也可以使用 cfg_attr 設置另一個基于 cfg 變量的屬性:

    #[cfg_attr(a, b)]

如果 a 使用 cfg 屬性設定,和使用 #[b] 是相同的。

cfg!

cfg! 語法擴展允許你在你代碼中的任何位置使用這些類型標記:

    if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
    println!("Think Different!");
    }

根據(jù)配置設置不同,這些在編譯時取真或假。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號