Do optional dependencies get enabled by default?

戏子无情 提交于 2020-03-17 03:12:06

问题


If I define a dependency like foo = { version = "1.0.0", optional = true }, will it be available when I do "cargo run"? Can I check if it is enabled in the code?

if cfg!(feature = "foo") {}

Doesn't seem to be working, like the feature is missing all the time.


回答1:


Moving answer to 60258216 here:

Optional dependencies do double as features: https://stackoverflow.com/a/39759592/8182118

They will not be enabled by default unless they're listed in the default feature, though you can enable the feature using cargo run --features foo.

For clarity and forward compatibility you may want to create an actual feature which enables the dependency though, that way if you need to "fluff up" the feature in the future and that requires new optional dependencies it's much easier.

In the code, both #[cfg] and cfg! should work depending whether you want to check for it at compile-time or runtime.

It's not hard to test either:

[package]
name = "testx"
version = "0.1.0"
edition = "2018"

[features]
default = ["boolinator"]
magic = ["boolinator"]
empty = []

[dependencies]
boolinator = { version = "*", optional = true }

and a main.rs of:

fn main() {
    # macro and attributes would work the same here
    if cfg!(feature = "boolinator") {
        println!("Hello, bool!");
    } else {
        println!("Hello, world!");
    }
}

you get

$ cargo run -q
Hello, bool!
$ cargo run -q --no-default-features
Hello, world!
$ cargo run -q --no-default-features --features boolinator
Hello, bool!
$ cargo run -q --no-default-features --features magic
Hello, bool!
$ cargo run -q --no-default-features --features empty
Hello, world!

See also https://github.com/rust-lang/edition-guide/issues/96



来源:https://stackoverflow.com/questions/60253288/do-optional-dependencies-get-enabled-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!