如果禁用相关功能,如何跳过依赖项

时间:2016-08-29 16:26:11

标签: dependencies rust conditional-compilation rust-cargo rust-crates

假设我有一个箱子,只有在glob启用时才依赖#[cfg(feature = "glob")]箱子。此外,默认情况下禁用此功能。如何在默认情况下跳过下载和编译glob包?

# Cargo.toml
...
[features]
default = []

[dependencies]
glob = "0.2"
...

源代码:

# lib.rs
.. several uses

#[cfg(feature = "glob")]
extern crate glob;

... a lot of code that doesn't use glob crate.

#[cfg(feature = "glob")]
impl Foo for Bar { 
    // only this code uses glob crate 
}

1 个答案:

答案 0 :(得分:5)

必须将glob依赖项标记为可选:

[dependencies]
glob = { version = "0.2", optional = true }