---
name: rust-module-analyzer
description: "An architecture read of an existing Rust module: its public surface, the crates it leans on, every unsafe block with its justification checked, and refactors ranked by payoff, as markdown, JSON, or plain text. Use when an existing Rust module needs a review of its public API, crate dependencies, unsafe blocks, and refactoring opportunities before it is changed or merged. Do not use for generating a new module, writing its documentation, or rewriting it for speed. Related topics: coding, rust."
---

# Rust Module Analyzer

An architecture read of an existing Rust module: its public surface, the crates it leans on, every unsafe block with its justification checked, and refactors ranked by payoff, as markdown, JSON, or plain text.

## When to use

Use this skill when an existing Rust module needs a review of its public API, crate dependencies, unsafe blocks, and refactoring opportunities before it is changed or merged. Do not use it for generating a new module, writing its documentation, or rewriting it for speed.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. List every pub item with its signature and flag any whose visibility is wider than its callers need.
3. Map the crates and std modules the code imports, noting any dependency used by a single function.
4. Check each unsafe block against its SAFETY comment and name the invariant at risk where the comment is missing or unsupported.
5. Note performance and documentation findings for the selected focus areas, each tied to a line range.
6. Rank refactoring suggestions by payoff over effort and format the report in the chosen output format.

## Inputs

- **Module source** (longer free text)
- **Include test analysis** (choose one: Yes, No)
- **Rust edition** (choose one: 2015, 2018, 2021, 2024)
- **Depth** (choose one: Summary, Detailed, Exhaustive)
- **Focus areas** (choose any: Public API, Dependency graph, Unsafe blocks, Performance, Documentation)
- **Output format** (choose one: Markdown, JSON, Plain text)

## Output

A report in the chosen format with Overview, Public API, Dependencies, Safety Review, Refactoring Suggestions, and Parsing Notes, at the selected depth.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Rust Module Analysis

Analyse this Rust module.

- Module source: {{Module source}}
- Include test analysis: {{Include test analysis}}
- Rust edition: {{Rust edition}}
- Depth: {{Depth}}
- Focus areas: {{Focus areas}}
- Output format: {{Output format}}

Begin with the public surface: every pub item with its signature, and whether the visibility is wider than its use warrants. Map the external crates and std modules the code imports and note any crate pulled in for one function. Review each unsafe block against its SAFETY comment; where the comment is missing or the invariant is not upheld by the surrounding code, say exactly which invariant is at risk. For performance, look for clones that could be borrows, allocation in loops, and locks held across awaits. For documentation, list the public items without doc comments. When tests are included, say what the tests cover and which public items have none. Rank the refactoring suggestions by payoff over effort and tie each to a line range. If part of the code does not parse, record the location and continue with the rest.

Return a report in the selected format with the sections Overview, Public API, Dependencies, Safety Review, Refactoring Suggestions, and Parsing Notes, keeping the depth the user chose.

## Pitfalls

- Treating every unsafe block as a defect instead of checking whether its invariant is actually upheld.
- Reporting a clone as wasteful when the value is moved into a thread or future and the clone is required.
- Recommending pub(crate) on an item that a downstream crate already uses, which is a breaking change.
