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会发现,报错。

正确的编译方式如下:

cargo build --features some_condition
本作品采用《CC 协议》,转载必须注明作者和本文链接
令狐一冲
本帖由系统于 3年前 自动加精
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
文章
255
粉丝
120
喜欢
308
收藏
128
排名:335
访问:2.8 万
私信
所有博文
社区赞助商