rustfmt after Rust edit
Runs rustfmt on Rust files after Claude edits them.
PostToolUseEdit|Writelow riskneeds manual checkWhat it does
Gates on .rs files and runs rustfmt on the edited file only, so large workspaces stay fast.
When it runs
| Event | PostToolUse |
| Matcher | Edit|Write |
| Scope | project |
| Platforms | macos, linux |
| Timeout | 30s |
| Requires | jq rustfmt |
| Failure mode | observe |
| Verified against | Claude Code hooks reference, event/matcher schema checked 2026-09-10 |
| Test status | needs manual check - run the test steps below before relying on it |
Config
Merge this into .claude/settings.json - or use the generator to combine hooks.
{
"hooks": {
"PostToolUse": [
{
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/rustfmt-after-edit.sh",
"timeout": 30
}
],
"matcher": "Edit|Write"
}
]
}
}Script
Save as .claude/hooks/rustfmt-after-edit.sh and chmod +x it.
#!/bin/bash
# rustfmt-after-edit: run after Claude edits a file. Reads hook JSON from stdin.
set -u
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ -f "$file" ] || exit 0
case "$file" in
*.rs) ;;
*) exit 0 ;;
esac
if ! command -v rustfmt >/dev/null 2>&1; then
exit 0 # formatter not installed; stay silent
fi
rustfmt "$file" >/dev/null 2>&1
exit 0
Install
- Save the script to .claude/hooks/rustfmt-after-edit.sh and run: chmod +x .claude/hooks/rustfmt-after-edit.sh
- Install the formatter (rustfmt) and make sure it is on your PATH, plus jq.
- Add the config fragment to .claude/settings.json (or use the generator).
Test it
- Positive: ask Claude to edit a matching file (*.rs), then check the file was reformatted.
- Negative: ask Claude to edit a non-matching file (e.g. README.txt) - the formatter should not run (check with /doctor or debug logs).
Sources
- Claude Code hooks reference (checked 2026-09-10)
- Claude Code hooks guide (checked 2026-09-10)
Pro Pack (later)
Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.
Waitlist opens soon.