How to Document Public APIs in Rust

Best Practices

Document public Rust APIs by adding `///` doc comments with examples and running `cargo doc` to generate the reference.

Document public APIs in Rust by adding doc comments above public items and generating HTML with cargo doc. Use /// for single-line comments and /** */ for multi-line blocks, including examples prefixed with # to ensure they compile.

/// Calculates the sum of two integers.
///
/// # Examples
///
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

// Generate documentation
$ cargo doc --open