Documentation explains how code works and how other people can use it. In Rust, documentation follows a consistent layout, so learning to read one library’s pages helps with many others. section 13.0 section 13.1
Read Rust documentation by looking for:
- Structs and traits listed on the left side of a page. section 18.4
- Code examples on the right side, often using assert_eq! to show expected results without requiring println! output. section 13.1.1
- Search results from the page’s search bar; press S to return to the top and search again. section 13.1.2
- Documentation comments beginning with ///, which describe public code for readers outside the implementation. section 1.2
Checking an example with assert_eq!
A documentation example can verify behavior directly: assert_eq!(vec.len(), 2) states that the vector should contain two items. If the values differ, the program panics; if it does not panic, the demonstrated result holds. section 13.1.1
/// This is a struct that does nothing
pub struct DoesNothing {}
/// This struct only has one method.
pub struct PrintThing {}
impl PrintThing {
/// This function just prints a message.
pub fn prints_something() {
println!("I am printing something");
}
}
Inspect how /// comments attach explanations to structs, methods, and functions before Cargo turns them into documentation pages. section 18.4











