天天看點

Rust 在 cargo 中進行條件編譯

說明

在​​Rust 條件編譯​​中,我們簡單介紹了條件編譯,但是在例子中并沒有給出如何結合cargo進行編譯。下面,我們就介紹如何結合cargo進行編譯。

源碼

//src/main.rs
#[cfg(some_condition)]
fn conditional_function() {
    println!("condition met!");
}

fn main() {
    conditional_function();
    println!("Hello, world!");
}      

Cargo.toml檔案

在cargo.toml中增加如下:

[features]
some_condition = []      

編譯

cargo build --features some_condition