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

Iva*_*hko 2 dependencies conditional-compilation rust rust-crates rust-cargo

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

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

[dependencies]
glob = "0.2"
...
Run Code Online (Sandbox Code Playgroud)

和源代码:

# 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 
}
Run Code Online (Sandbox Code Playgroud)

mca*_*ton 5

glob依赖必须标记为可选:

[dependencies]
glob = { version = "0.2", optional = true }
Run Code Online (Sandbox Code Playgroud)