I am writing documentation for a module that has some options controlled by a Cargo feature flag. I'd like to always show this documentation so that consumers of the crate know that it's available, but I need to only run the example when the feature is enabled.
lib.rs
//! This crate has common utility functions
//!
//! ```
//! assert_eq!(2, featureful::add_one(1));
//! ```
//!
//! You may also want to use the feature flag `solve_halting_problem`:
//!
//! ```
//! assert!(featureful::is_p_equal_to_np());
//! ```
pub fn add_one(a: i32) -> i32 {
a + 1
}
#[cfg(feature = "solve_halting_problem")]
pub fn is_p_equal_to_np() -> bool {
true
}
Cargo.toml
[package]
name = "featureful"
version = "0.1.0"
authors = ["An Devloper <an.devloper@example.com>"]
[features]
solve_halting_problem = []
[dependencies]
Running with the feature enabled runs both doctests as expected:
$ cargo test --features=solve_halting_problem
Doc-tests featureful
running 2 tests
test src/lib.rs - (line 7) ... ok
test src/lib.rs - (line 3) ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Running without the feature has an error:
$ cargo test
Doc-tests featureful
running 2 tests
test src/lib.rs - (line 7) ... FAILED
test src/lib.rs - (line 3) ... ok
failures:
---- src/lib.rs - (line 7) stdout ----
error[E0425]: cannot find function `is_p_equal_to_np` in module `featureful`
--> src/lib.rs:8:21
|
4 | assert!(featureful::is_p_equal_to_np());
| ^^^^^^^^^^^^^^^^ not found in `featureful`
Both the ```ignore
and ```no_run
modifiers apply when the feature is enabled or not, so they don't seem to be useful.
How would one achieve conditional compilation with Rust projects that have doctests? is close, but the answer focuses on functions that change with conditional compilation, not on documentation of modules.