Skip to content

Instantly share code, notes, and snippets.

@jam1garner
Last active October 31, 2021 22:30
Show Gist options
  • Save jam1garner/7d909e1d0e5986b9b8d50830314bec63 to your computer and use it in GitHub Desktop.
Save jam1garner/7d909e1d0e5986b9b8d50830314bec63 to your computer and use it in GitHub Desktop.
How to setup doc_cfg for Rust to document your cfg features (credit to benesch and mcarton on stackoverflow)
  1. Setup the nightly feature in your lib.rs (lib.rs)
  2. Add a copy of your feature toggle (other_file.rs)
  3. Setup your Cargo.toml to have docs.rs enable the conditional compilation from the first 2 steps (Cargo.toml)

Note: step #2 is now only necessary in situations in which rustdoc currently cannot tell cfg() applies to an item (for example a public item inside a private module which is itself cfg'd which then is re-exported). See this issue for more info.

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "my-feature")]
#[cfg_attr(docsrs, doc(cfg(feature = "my-feature")))]
fn my_func_behind_feature() {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment