How to Use cfg_attr to Conditionally Document Features

Use #[cfg_attr(feature = "name", doc = "text")] to add documentation only when a specific feature is enabled.

Use the #[cfg_attr] attribute on your item to conditionally apply the doc attribute when a specific feature is enabled. Place #[cfg_attr(feature = "your_feature", doc = "Your documentation text")] immediately before the item definition to include the documentation only when that feature is active.

#[cfg_attr(feature = "nightly", doc = "This is only documented when the nightly feature is enabled.")]
pub struct MyStruct {
    pub value: i32,
}

Alternatively, to conditionally include the entire item in documentation (and compilation), use #[cfg_attr(feature = "nightly", doc(hidden))] to hide it or #[cfg(feature = "nightly")] to exclude it entirely from builds without the feature.