| name | rust-doc-comments |
| description | Use when adding, reviewing, or improving doc-comments on Rust functions, methods, structs, enums, or modules. Covers idiomatic `///` and `//!` style, required sections, and automation via cargo doc. |
| allowed-tools | bash, read, edit, grep |
Rust Doc Comments
Overview
Add idiomatic Rust documentation comments (///) to all public — and relevant private — functions, methods, structs, enums, traits, and modules. Follow standard Rustdoc conventions so cargo doc generates correct, navigable HTML.
When to Use
- Adding doc-comments to an undocumented Rust project
- Reviewing quality of existing doc-comments
- Deciding which sections (
# Arguments, # Returns, etc.) a comment needs
- Generating or verifying doctests compile
Step-by-Step Process
1. Discover undocumented items
cargo rustdoc -- -D rustdoc::missing_doc_code_examples 2>&1 | head -60
RUSTDOCFLAGS="-D missing_docs" cargo doc 2>&1 | grep "warning\|error"
grep -n "pub fn\|pub struct\|pub enum\|pub trait" src/**/*.rs
2. Write the comment
Place /// immediately above the item — no blank line between comment and item.
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
3. Section rules — include only what applies
| Section | Include when |
|---|
# Arguments | Function has ≥ 1 parameter worth explaining |
# Returns | Return type is non-obvious or has special cases |
# Errors | Function returns Result<_, E> |
# Panics | Function can panic (even via unwrap) |
# Safety | Function is unsafe — required |
# Examples | Any public item; doctests are run by cargo test |
Skip sections that add no information. # Returns for () is noise.
4. Module-level and crate-level docs
Use //! (inner doc comment) at the top of lib.rs / main.rs / mod.rs:
5. Verify
cargo doc --no-deps 2>&1 | grep -E "warning|error"
cargo test --doc
Quick Reference
pub fn do_thing() {}
pub fn do_thing() {}
fn retry_with_backoff(f: impl Fn() -> bool) -> bool { ... }
pub struct Config {
pub max_retries: u32,
}
pub enum Status {
Ok,
Error(u16),
}
Common Mistakes
| Mistake | Fix |
|---|
// comment instead of /// comment | Use /// for rustdoc |
Blank line between /// and item | Remove the blank line |
# Arguments for self-evident params | Skip obvious parameters |
Doctest not in a ```rust block | Add rust language tag |
| Doctest uses private API | Mark with ```rust,ignore or use pub |
Missing # Errors on Result-returning fn | Always document error variants |
Missing # Safety on unsafe fn | Required — document invariants |
| Outdated docs after refactor | Re-run cargo doc and cargo test --doc |
Automation Tips