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