Sub topic 5
Linking to other items
This chapter assumes you can write /// and //! comments, format them with Markdown, and run cargo doc. Here you will link from one piece of documentation to another item, such as a function, type, or module. Rustdoc checks these links when it builds the docs.
Intra-doc links
An intra-doc link uses an item's Rust path as the link target. You put the path in square brackets. Rustdoc looks the path up and replaces it with a real URL.
//! Port utilities.
use std::num::ParseIntError;
pub mod config {
/// The port used when none is given.
pub const DEFAULT_PORT: u16 = 8080;
}
/// Parses a port number.
///
/// Use [`config::DEFAULT_PORT`] when the input is empty.
/// Failures are reported as [`ParseIntError`].
pub fn parse_port(s: &str) -> Result<u16, ParseIntError> {
s.trim().parse()
}
/// A validated port.
pub struct Port(pub u16);
impl Port {
/// Builds a port with [`parse_port`] and wraps it.
///
/// See also [`Self::get`].
pub fn parse(s: &str) -> Result<Port, ParseIntError> {
parse_port(s).map(Port)
}
/// Returns the number inside this [`Port`].
pub fn get(&self) -> u16 {
self.0
}
}Backticks inside the brackets are optional. They make the link text render as code, which is usually what you want for item names.
How paths are resolved
Rustdoc resolves a path the way the compiler would at that spot in the code. Names that are in scope work as written. That is why ParseIntError works above: the file imports it with use.
- Paths can start with
crate::,self::, orsuper::. SelfandSelf::methodwork insideimplblocks and on types.- Standard library items such as [
Vec] resolve, even when you have not imported them. - Items from dependencies resolve by the crate's name, for example
serde::Serialize.
You can also give the link your own text, or use a reference-style link.
/// Reads a [port number](parse_port) from a string.
///
/// The parser is described in [`parse_port`][pp].
///
/// [pp]: crate::parse_port
pub fn read(_s: &str) {}Add //! link definitions at the top of a file if several comments share the same target.
flowchart TDDisambiguating names
Rust keeps types, functions, and macros in separate namespaces. A struct and a function can share a name. If a path is ambiguous, rustdoc warns and asks you to pick one.
| Form | Meaning |
|---|---|
| `struct@Name` | A struct. Also `enum@`, `trait@`, `union@`, `type@`. |
| `mod@name` | A module. |
| `fn@name` or `name()` | A function or method. |
| `macro@name` or `name!` | A macro. |
| `const@NAME`, `static@NAME` | A constant or a static. |
For example, `[vec!] links to the macro, and [fn@parse_port]` forces the function.
Catching broken links
Rename or remove an item and any link to it stops resolving. Rustdoc reports this as a warning from the rustdoc::broken_intra_doc_links lint. You can turn it into an error at the top of lib.rs:
#![deny(rustdoc::broken_intra_doc_links)]A related lint, rustdoc::private_intra_doc_links, warns when public documentation links to a private item. Readers cannot follow that link, because private items are not in the public docs.
Run cargo doc --no-deps and read the warnings. A link written as plain [Name] with no matching item is not an error. Rustdoc leaves the text as it is.
Exercise
In the impl Port block, what should you write in a doc comment to link to the get method of the same type, and which lint fires if the link no longer resolves?
Show answer
Write [Self::get] (or [Port::get]). If it stops resolving, rustdoc emits a warning from the rustdoc::broken_intra_doc_links lint.
Exercise
A module contains both struct Config and fn Config(). How do you make a doc link point to the struct?
Show answer
Use a disambiguator: [struct@Config].