Overview
Rustdoc extracts documentation comments from Rust code and generates comprehensive, searchable HTML documentation. It’s the tool behind all the documentation you see at docs.rs and the standard library documentation.Rustdoc is tightly integrated with the Rust compiler and shares much of its infrastructure, located in
src/librustdoc/ and the tool wrapper in src/tools/rustdoc/.Architecture
Rustdoc consists of two main components:1. The Library (librustdoc)
Located at src/librustdoc/, this contains the core documentation generation logic:
- AST Processing: Analyzes Rust code structure
- Markdown Rendering: Converts doc comments to HTML
- HTML Generation: Creates styled, interactive documentation pages
- Search Index: Builds JavaScript search functionality
2. The Tool Binary
The thin wrapper insrc/tools/rustdoc/ provides the command-line interface:
Building Rustdoc
Rustdoc is built as part of the Rust compiler toolchain:Build the compiler
Rustdoc requires
rustc_private features, so it depends on the compiler being built first.Package Configuration
Fromsrc/tools/rustdoc/Cargo.toml:
Documentation Comments
Rustdoc processes several types of documentation comments:Item Documentation
Module Documentation
Key Features
Automatic Links
Rustdoc automatically generates links between types and functions.
Doc Tests
Code blocks in documentation are automatically tested.
Search
Generates a JavaScript-based search index for fast lookup.
Source Links
Links to source code for each documented item.
Usage
Basic Documentation Generation
Command-Line Options
Advanced Features
Cross-Crate Documentation
Rustdoc can link to documentation from dependencies:Documentation Attributes
Control documentation generation with attributes:Custom CSS and Themes
Rustdoc supports custom styling:HTML Template System
Rustdoc uses a template system for HTML generation located insrc/librustdoc/html/templates/:
Static Assets
Rustdoc includes various static assets:- CSS: Styling for light/dark themes
- JavaScript: Search functionality and interactivity
- Fonts: Custom fonts like Source Serif 4
JSON Output Format
Rustdoc can generate machine-readable JSON:src/rustdoc-json-types/.
Testing Documentation
Rustdoc includes several testing tools:Doc Tests
Specialized Test Tools
jsondocck: Tests JSON output formatjsondoclint: Lints JSON documentationrustdoc-gui-test: GUI testing for rustdoc outputhtml-checker: Validates generated HTML
Performance
Rustdoc can be memory-intensive for large projects:Resources
- Rustc Dev Guide: rustc-dev-guide.rust-lang.org/rustdoc.html
- The Rustdoc Book: doc.rust-lang.org/rustdoc
- Source Code:
src/librustdoc/in the Rust repository