rustfmt 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

rustfmt 命令安装:


-bash/zsh: rustfmt command not found

# On the Stable toolchain
rustup component add rustfmt # To install:
cargo fmt # To run on a cargo project in the current working directory:

# On the Nightly toolchain
rustup component add rustfmt --toolchain nightly # To install:
cargo +nightly fmt # To run on a cargo project in the current working directory:

# Arch Linux
pacman -S rustfmt-preview

# Fedora
dnf install rustfmt

# Dockerfile
dockerfile.run/rustfmt

rustfmt 命令补充说明:


rustfmt 被设计得非常可配置。 可以创建一个名为 rustfmt.toml.rustfmt.toml 的 TOML 文件,将其放置在项目或任何其他父目录中,它将应用该文件中的选项。

如果这些目录都不包含此类文件,则还会检查您的主目录和全局配置目录中名为 rustfmt 的目录(例如 .config/rustfmt/)。

rustfmt 命令语法:


rustfmt [options] ... 

rustfmt 命令选项:


--check         Run in 'check' mode. Exits with 0 if input is 
                formatted correctly. Exits with 1 and prints a diff if 
                formatting is required. 
--emit [files|stdout] 
                What data to emit and how 
--backup        Backup any modified files. 
--config-path [Path for the configuration file] 
                Recursively searches the given path for the 
                rustfmt.toml config file. If not found reverts to the 
                input file path 
--edition [2015|2018|2021] 
                Rust edition to use 
--color [always|never|auto] 
                Use colored output (if supported) 
--print-config [default|minimal|current] PATH 
                Dumps a default or minimal config to PATH. A minimal 
                config is the subset of the current config file used 
                for formatting the current program. `current` writes 
                to stdout current config as if formatting the file at 
                PATH. 
-l, --files-with-diff  
                Prints the names of mismatched files that were 
                formatted. Prints the names of files that would be 
                formatted when used with `--check` mode. 
--config [key1=val1,key2=val2...] 
                Set options from command line. These settings take 
                priority over .rustfmt.toml 
-v, --verbose       Print verbose output 
-q, --quiet         Print less output 
-V, --version       Show version information 
-h, --help [=TOPIC] Show this message or help about a specific topic: 
                `config` 

rustfmt 命令实例:


rustfmt 格式化文件,就地覆盖原始文件:

rustfmt path/to/source.rs

rustfmt 检查文件的格式并在控制台上显示任何更改:

rustfmt --check path/to/source.rs

rustfmt 在格式化之前备份所有修改过的文件(原始文件使用 .bk 扩展名重命名):

rustfmt --backup path/to/source.rs

rustfmt 命令扩展阅读:




rustfmt 命令评论