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

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

假设我有一个 crate ,仅当启用 #[cfg(feature = "glob")] 时,该 crate 才依赖于 glob crate 。此外,该功能默认处于禁用状态。如何默认跳过 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 
}

最佳答案

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

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

关于dependencies - 如果相关功能被禁用,如何跳过依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210887/

相关文章:

java - 如何修复全部 "com.android.support libraries must use the exact same version "

java - 平台强制版本控制机制是 Java 最需要的特性吗?

error-handling - Option<T> 的 map_err

c - 在 C 中使用 #ifdef 和 enum

rust - cargo : How to include the entire directory or file in feature flags?

ruby-on-rails - 从 Rails : dependency issues 调用外部 rake 任务

java - Gradle:从运行时依赖项中排除 jar

rust - 如何通过读取和转换文件来创建Stream?

rust - 有没有办法获取特征的类型名称?

c++ - Qt pro 文件条件库