ConfigsJul 21, 2026·3 min read

rustfmt — The Official Rust Code Formatter

The official Rust source code formatter that enforces a consistent style across your codebase, integrated into cargo and CI pipelines.

Agent ready

Ready-to-run agent install

This asset can be installed after the agent chooses its runtime, checks the plan, and runs the matching command.

Native · 98/100Policy: allow
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: Established
Entrypoint
rustfmt Overview
Direct install command
npx -y tokrepo@latest install 549d42de-84e1-11f1-9bc6-00163e2b0d79 --target codex

Run after dry-run confirms the install plan.

Introduction

rustfmt is the official code formatter for Rust, maintained by the Rust project. It automatically reformats Rust source code to conform to the official Rust style guide, eliminating style debates and ensuring consistency across teams and open-source projects.

What rustfmt Does

  • Formats Rust source files to follow the official Rust style guide
  • Integrates with cargo fmt for project-wide formatting in a single command
  • Supports a --check mode that returns a non-zero exit code on unformatted code for CI gating
  • Handles complex Rust syntax including macros, attributes, and where clauses
  • Reads configuration from a rustfmt.toml file for project-level customization

Architecture Overview

rustfmt parses Rust source code using the compiler's own parser (via rustc internals) to produce an AST. It then walks the AST and emits reformatted source text, preserving comments and attributes. The formatter handles whitespace, line breaking, and indentation according to configurable rules. It is distributed as a rustup component and invoked through the cargo toolchain.

Self-Hosting & Configuration

  • Installed automatically with most Rust toolchains; add manually with rustup component add rustfmt
  • Configure via rustfmt.toml or .rustfmt.toml in the project root
  • Key options include max_width (default 100), tab_spaces, edition (2015/2018/2021), and use_small_heuristics
  • Run on save in editors via rust-analyzer integration
  • Use --edition 2021 flag to match your project's Rust edition

Key Features

  • Zero-config defaults that match the official Rust style guide
  • Stable and nightly channels with different sets of configurable options
  • Preserves meaningful blank lines and respects #[rustfmt::skip] attributes
  • Formats code inside doc comments when using nightly
  • Deterministic output ensures the same code always produces the same formatting

Comparison with Similar Tools

  • Prettier — JavaScript/TypeScript formatter; rustfmt fills the same role for Rust
  • Black — Python formatter with a similar zero-config philosophy
  • gofmt — Go's built-in formatter; rustfmt was inspired by gofmt's approach
  • clang-format — C/C++ formatter; more configurable but requires more setup than rustfmt

FAQ

Q: Can I override the default style? A: Yes. Place a rustfmt.toml in your project root. Some options are stable; others require the nightly toolchain.

Q: How do I skip formatting for a block of code? A: Add #[rustfmt::skip] before the item, or use // rustfmt::skip for expressions.

Q: Does rustfmt work with macros? A: rustfmt formats most macro invocations. Complex procedural macros may be left unformatted.

Q: Should I run rustfmt in CI? A: Yes. Use cargo fmt -- --check in CI to enforce consistent formatting across all contributions.

Sources

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets