24

I'm writing a library in Cargo. If this library depends on another library like libc, which exposes a feature (in this case, use_std), how do I make a feature I expose enable or disable that feature in my dependency?

Looking at the cargo documentation, it looks like there's no official way specified to do this.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
burtonageo
  • 397
  • 2
  • 7
  • 2
    This question has a better answer: [How to pass compiler flags to a sub crate in Rust?](https://stackoverflow.com/a/50986237/4934640) – Evandro Coan Apr 28 '20 at 05:56

1 Answers1

26

From the documentation you linked to:

# Features can be used to reexport features of other packages. The `session`
# feature of package `awesome` will ensure that the `session` feature of the
# package `cookie` is also enabled.
session = ["cookie/session"]

Is that sufficient?

Wez Furlong
  • 4,727
  • 1
  • 29
  • 34
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    Hmm, what if I have a dependency like [`ascii`](https://github.com/tomprogrammer/rust-ascii), which exposes a `no_std` flag - which I want to _disable_ if my `use_std` flag is enabled? – burtonageo Oct 13 '16 at 13:08
  • 1
    @burtonageo an interesting question, you could probably ask a separate top-level question for that! It might even be a feature request. Specifically for `no_std` though, I feel like I heard that the right thing to do is to have a *default feature* that enables using things from `std`, so perhaps the ascii crate is just backwards... – Shepmaster Oct 13 '16 at 13:22
  • 1
    Yeah, I've heard that features are meant to be additive, not subtractive, so I think my best bet would be to make a pull request to them. – burtonageo Oct 13 '16 at 13:54