cargo doc generates HTML documentation from your Rust code and doc comments. It's built into Cargo - no plugins or configuration needed.
# Generate documentation
cargo doc
# Generate and open in browser
cargo doc --open
# Include private items
cargo doc --document-private-itemsUse /// for documentation comments (these become part of the generated docs):
/// Parses a JSON string and returns a JsonValue.
///
/// # Arguments
/// * `input` - A string slice containing valid JSON
///
/// # Returns
/// * `Ok(JsonValue)` - Successfully parsed value
/// * `Err(JsonError)` - Parse error with position info
///
/// # Example
/// ```
/// let value = parse_json("42").unwrap();
/// ```
pub fn parse_json(input: &str) -> Result<JsonValue, JsonError> {
// ...
}Use //! at the top of a file for module documentation:
//! JSON parser library.
//!
//! This crate provides tokenization and parsing of JSON primitives.
pub mod error;
pub mod parser;Running cargo doc --open generates:
- Browsable HTML documentation
- Automatic linking between types and functions
- Search functionality
- Source code links
- Organized by modules
Documentation is generated in:
target/doc/<crate_name>/index.html
- Zero config - Works out of the box
- Consistent style - All Rust docs look the same
- Code examples are tested - Doc tests run with
cargo test - Part of the ecosystem - Same format as docs.rs
cargo doc --open # Generate and view
cargo test --doc # Run doc tests only
cargo doc --no-deps # Skip dependencies (faster)For our JSON parser, cargo doc --open generates docs showing:
JsonErrorenum with all variantsJsonValueenum with all variantsparse_json()function with signaturetokenize()function with signature- Module hierarchy (error, parser, tokenizer, value)