I have a Rust project that follows a structure:
- main_crate (features = "x")
- driver_crate (features = "x")
- x_crate
...
...
In my main_crate
, I have a feature flag x
that I enable by default which is propagated to the driver_crate
via the following setup in main_crate/Cargo.toml
:
[features]
x = ["driver-crate/x"]
# default = ["x"]
The driver_crate/Cargo.toml
specifies the x
dependency as optional in the following way:
[dependencies]
x-crate = { path = "../x-crate", optional = true }
[features]
x = ["dep:x-crate"]
Suppose that I run cargo build --no-default-features
, my expectation is that now that x
is no longer enabled by default, Cargo should now treat the x
dependency as false
and hence not even attempt to compile it as part of the project. Instead, cargo still adds x_crate
as part of the compilation process.
Is there a way to ensure that x_crate
is not compiled at all when feature x
is not specified?