← 返回命令列表

Linux command

rustfmt 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Format a Rust file

rustfmt [main.rs]

Format multiple files

rustfmt [src/*.rs]

Check formatting

rustfmt --check [main.rs]

Format with backup

rustfmt --backup [main.rs]

Format using specific edition

rustfmt --edition [2021] [main.rs]

Format from stdin

cat [main.rs] | rustfmt

Show diff

rustfmt --emit diff [main.rs]

Use config file

rustfmt --config-path [rustfmt.toml] [main.rs]

说明

rustfmt is the official Rust code formatter. It automatically formats Rust code according to style guidelines, ensuring consistent code style across a project. The tool can be integrated into editors, CI pipelines, and pre-commit hooks. Configuration is stored in rustfmt.toml or .rustfmt.toml at the project root. When used with Cargo (`cargo fmt`), it formats all files in the project automatically. rustfmt supports all stable Rust syntax and can be configured for various style preferences.

参数

--check
Check if formatting is correct, don't modify.
--backup
Create backup of original files.
--edition _year_
Rust edition (2015, 2018, 2021, 2024).
--emit _type_
Output mode (files, stdout, diff).
--config _key=value_
Set configuration option.
--config-path _file_
Path to configuration file.
--print-config _type_
Print configuration (default, current, minimal).
--files-with-diff
Print files that would change.
--verbose
Print verbose output.
--quiet
Print minimal output.
--color _when_
Color output (auto, always, never).

FAQ

What is the rustfmt command used for?

rustfmt is the official Rust code formatter. It automatically formats Rust code according to style guidelines, ensuring consistent code style across a project. The tool can be integrated into editors, CI pipelines, and pre-commit hooks. Configuration is stored in rustfmt.toml or .rustfmt.toml at the project root. When used with Cargo (`cargo fmt`), it formats all files in the project automatically. rustfmt supports all stable Rust syntax and can be configured for various style preferences.

How do I run a basic rustfmt example?

Run `rustfmt [main.rs]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --check do in rustfmt?

Check if formatting is correct, don't modify.