Do optional dependencies get enabled by default?

2020-05-10 06:41发布

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条回答
够拽才男人
2楼-- · 2020-05-10 07:26

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

查看更多
登录 后发表回答