3.2. 使用 Rustfmt 作为独立工具
使用
Rustfmt 作为独立工具来格式化 Rust 源文件及其所有依赖项。作为替代方案,在 Cargo
构建工具中使用 Rustfmt。如需更多信息,请参阅使用带有 Cargo 的 Rustfmt。
先决条件
-
现有的 Rust 项目。
有关如何创建 Rust 项目的详情,请参考 创建 Rust 项目。
流程
要使用 Rustfmt 作为独立 工具
格式化 Rust 源文件,请运行以下命令:
在 Red Hat Enterprise Linux 8 中:
$ rustfmt <source-file>
-
将
<source_file
> 替换为源文件的名称。
另外,您可以将 <source_file>
; 替换为标准输入。Rust
fmt 随后在标准输出中提供其输出。
-
将
在 Red Hat Enterprise Linux 9 中:
$ rustfmt <source-file>
-
将
<source_file
> 替换为源文件的名称。
另外,您可以将 <source_file>
; 替换为标准输入。Rust
fmt 随后在标准输出中提供其输出。
-
将
注意
默认情况下,Rust
fmt 修改受影响的文件,而不显示详情或创建备份。要显示详情并创建备份,请使用 --write-mode 值运行 Rustfmt
。