| name | rust-api-doc |
| description | Add comprehensive API documentation to Rust code for docs.rs. Use this skill when the user asks to "document this code", "add doc comments", "improve documentation", "make this docs.rs ready", "add rustdoc", or mentions preparing code for publication. Also trigger when the user is working on a library crate and mentions documentation, API clarity, or user-facing interfaces. |
| disable-model-invocation | true |
Rust API Documentation
Add comprehensive, user-friendly documentation comments to Rust code following docs.rs best practices.
Core Principles
Explain why, not what: The code already shows what it does. Documentation should explain why it exists, when to use it, and how it fits into the larger picture.
Examples for user-facing APIs: Public APIs that users interact with directly should include examples. Internal helpers and obvious utilities don't need them.
Cross-link related types: Use [TypeName] to create clickable links in docs.rs. Help users discover related functionality.
Document invariants and edge cases: What assumptions does the code make? What happens in unusual situations?
What to Document
Module-level (//!)
- Purpose of the module
- Key concepts and relationships
- Usage examples for the primary workflow
- Links to important types
Public structs and enums
- What the type represents (domain concept, not implementation)
- When to use it vs alternatives
- Key invariants or constraints
- Example for non-obvious usage
Public functions
- What problem it solves (not just what it does)
- When to use it
- Errors it can return and why
- Examples for complex APIs
Fields (when public)
- What the field represents
- Valid ranges or constraints
- How it relates to other fields
Documentation Style
Structure
Sections to use
# Example / # Examples — concrete usage
# Errors — what can go wrong
# Panics — when it panics
# Safety — for unsafe code
# Invariants — constraints that must hold
Writing style
- Use present tense: "Returns the value" not "Will return"
- Be direct: "Creates a new instance" not "This function creates"
- Explain trade-offs: "Faster but uses more memory"
- Link related items: "See also [
OtherType]"
What NOT to Document
Skip documentation for:
- Private items (unless they're complex and need explanation for maintainers)
- Obvious getters/setters
- Standard trait implementations (Debug, Clone, etc.) unless they have special behavior
- Test functions
Process
- Read the entire file to understand the module's purpose and relationships
- Identify user-facing APIs — public items that external users interact with
- Add module-level docs explaining the big picture
- Document public types with focus on why/when, not what
- Add examples for non-obvious public APIs
- Cross-link related types to help users discover functionality
- Verify the documentation compiles with
cargo doc
Example Transformations
Before
pub struct Task {
pub id: String,
pub status: Status,
}
pub fn run(task: &Task) -> Result<()> {
}
After
pub struct Task {
pub id: String,
pub status: Status,
}
pub fn run(task: &Task) -> Result<()> {
}
Common Patterns
Explaining relationships
Documenting invariants
pub bindings: HashMap<String, String>,
Explaining design decisions
Verification
After documenting, run:
cargo doc --no-deps --document-private-items
Check for:
- Broken links (warnings about unresolved links)
- Malformed code blocks
- Missing backticks around type names
Open the generated docs in a browser to verify examples render correctly and links work.