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.
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 usingcargo 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]
andcfg!
should work depending whether you want to check for it at compile-time or runtime.It's not hard to test either:
and a main.rs of:
you get
See also https://github.com/rust-lang/edition-guide/issues/96