Sub topic 12
The #[doc] attribute: hidden, alias, and more
This chapter assumes you can write /// and //! comments, run cargo doc, and know what pub use does. The #[doc] attribute gives you control over how rustdoc presents items, beyond what the comment text alone can do.
Doc comments are attributes
Every /// comment is sugar for a #[doc = "..."] attribute, and every //! comment is sugar for #![doc = "..."]. These two functions have identical documentation:
/// Returns the answer.
pub fn answer_a() -> u32 {
42
}
#[doc = " Returns the answer."]
pub fn answer_b() -> u32 {
42
}The attribute form matters because its value can come from somewhere else. The most common case is reusing your README as the crate documentation:
#![doc = include_str!("../README.md")]Rust code blocks in the README then become doc tests too, so the README examples stay correct.
The common options
| Attribute | Effect |
|---|---|
| `#[doc(hidden)]` | Leaves the item out of the generated documentation |
| `#[doc(alias = "name")]` | Makes the item findable in search under another name |
| `#[doc(inline)]` | Shows a re-exported item's full documentation at the re-export |
| `#[doc(no_inline)]` | Shows only a link to the original item |
| `#![doc(test(attr(...)))]` | Adds crate-level attributes to every doc test |
Hiding items
#[doc(hidden)] removes an item from the rendered docs and from search. The item stays pub and callers can still use it. Libraries use this for items that must be public for technical reasons, such as functions called by their own macros.
#[doc(hidden)]
pub fn __macro_support() {}Do not confuse this with the # prefix from the earlier chapter on hidden lines. That prefix hides lines inside one example. #[doc(hidden)] hides a whole item.
Adding search aliases
Readers often search for the name they know from another language or library. An alias lets them find your function under that name.
/// Returns the number of bytes in `items`.
#[doc(alias = "size")]
#[doc(alias = "count")]
pub fn len_of(items: &[u8]) -> usize {
items.len()
}Searching the generated docs for size or count now lists len_of. You can also write #[doc(alias("size", "count"))]. An alias is only a search hint. It does not create a second name you can call in code.
Controlling re-exports
A re-export is a pub use that exposes an item at a new path. By default, rustdoc shows a re-export of a public item as a link to the original. Add #[doc(inline)] to copy the item's documentation to the new location instead.
pub mod units {
/// A temperature in degrees Celsius.
pub struct Celsius(pub f64);
}
#[doc(inline)]
pub use units::Celsius;Here Celsius is documented at the crate root as if it were defined there. Use #[doc(no_inline)] for the opposite, such as when you re-export a type from another crate and do not want its documentation copied into yours.
Configuring doc tests
Crate-level doc(test(...)) settings apply to every doc test in the crate. This one makes any warning in an example a failure:
#![doc(test(attr(deny(warnings))))]Without it, an example that compiles with warnings still passes.
Exercise
A library has a public function __expand_helper that its own macro calls. You do not want it in the documentation. Which attribute do you use, and can users still call the function?
Show answer
Use #[doc(hidden)]. Users can still call it, because the attribute only affects the generated documentation and search, not visibility.
Exercise
You want readers who search for "length" to find your function byte_count. What do you add?
Show answer
Add #[doc(alias = "length")] above the function.