Additional dependency when feature is NOT enabled

Is it possible to enable a dependency in Cargo.toml only when a feature is not enabled?

I use no_std_compat for my no_std crate and I want to add a dependency when the std feature is not enabled.

I'd love to be proven wrong, but to my knowledge this isn't possible because there is no way to describe negation in terms of optional features.

So your best bet is to include the dependency unconditionally.

I think it's possible, but, Cargo features are additive throughout your entire crate tree, so it's rather important that they don't change things this way.

The standard pattern for optional no_std is to do the opposite of what you're saying: instead of a no_std feature gate, make std an optional dependancy and gate on that.

The problem with the negative feature, is if one crate goes "I don't need std, I'll enable the no_std_compat feature" it would now disable that functionality for every other crate in your dependency tree (which may depend on the functionality that got disabled)"

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.