No. Features are chosen by the end user of the crate, and you aren't the only person who chooses to run the tests. If you could do what you are asking, you'd actually force anyone who wanted to run the tests to download and compile the "optional" dependency, making it not very optional.
What you can do instead is only include that piece of the documentation when the feature is enabled. It's not obvious, but documentation comments are transformed into the attribute syntax (#[doc = "..."]
). Combined with cfg_attr
, you can conditionally include documentation, thus conditionally compile and run an example:
#![cfg_attr(feature = "alpha", doc = "
# Examples
```rust
fn alpha() {}
```
")]
Likewise, you can have the opposite case for a bit of documentation that says "check out this awesome feature!".
See also: