假设我有一个 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/