Example binary with non-default features

I'm adding an example binary to my library crate. I'd like to build that binary against a version of my library that has some non-default features enabled. How do I go about that? I haven't been able to figure it out from Cargo Targets - The Cargo Book.

Looks like you use the required-features key in your [[example]] (see the "Configuring a target" section on that page).

1 Like

Excellent, thanks! Here's what I wound up with:

[[example]]
name = "cureq"
required-features = ["charset", "cookies", "socks-proxy"]

And then I built with:

$ cargo build --example cureq 
error: target `cureq` in package `ureq` requires the features: `charset`, `cookies`, `socks-proxy`
Consider enabling them by passing, e.g., `--features="charset cookies socks-proxy"`

Then I built it with those flags and it worked:

$ cargo build --example cureq --features="charset cookies socks-proxy"

I don't suppose there's any way to make it so this doesn't require passing the extra flag, but just builds with those features by default?

Apparently not right now, see

But that issue has a link to this not-yet-accepted RFC that would make it possible:

1 Like