此内容没有您所选择的语言版本。

Chapter 3. The rustfmt formatting tool


With the rustfmt formatting tool, you can automatically format the source code of your Rust programs. You can use rustfmt either as a standalone tool or with Cargo.

For further details, see the rustfmt help pages displayed by the rustfmt --help command.

3.1. Installing rustfmt

Install the rustfmt formatting tool by installing the rustfmt package. On RHEL 8 enable the rust-toolset module; on RHEL 9 and 10 install the rust-toolset package.

Procedure

  • Install the rustfmt package:

    • On RHEL 8, enter:

      # yum install rustfmt
    • On RHEL 9 and 10, enter:

      # dnf install rustfmt

3.2. Using rustfmt as a standalone tool

Use rustfmt as a standalone tool to format a Rust source file and all its dependencies. As an alternative, use rustfmt with the Cargo build tool. For more information, see Using rustfmt with the Cargo build tool.

Prerequisites

Procedure

  • Format the Rust source code:

    $ rustfmt <source_file>

    Replace <source_file> with the name of your source file. If you use standard input instead, formatted output is printed to standard output.

    Important

    By default, rustfmt modifies the affected files without displaying details or creating backups. To display details and create backups, run rustfmt with the --write-mode option.

3.3. Using rustfmt with the Cargo build tool

Use the rustfmt tool with Cargo to format a Rust source file and all its dependencies. As an alternative, use rustfmt as a standalone tool. For more information, see Using rustfmt as a standalone tool.

Prerequisites

Procedure

  1. Optional: To change the rustfmt formatting options, create rustfmt.toml configuration file in the project directory and add your configurations to the file.
  2. Format the Rust source code:

    $ cargo fmt
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部