RustRover 2023.3 Help

Rustfmt

Rustfmt is a formatting tool that enforces the standard Rust-community code style. By default, Rustfmt is disabled. You can enable it, in which case the IDE will use Rustfmt instead of the built-in formatter when formatting Rust files and projects.

Enable (or disable) Rustfmt instead of the built-in formatter

  1. Open settings by pressing Control+Alt+S and navigate to Languages & Frameworks | Rust | Rustfmt

  2. To enable Rustfmt, set the Use rustfmt instead of built-in formatter checkbox. To disable Rustfmt, clear the checkbox.

  3. Click OK to apply the changes.

Automatically run Rustfmt on Save

You can configure RustRover to run Rustfmt for Rust files whenever they are saved:

  1. Open settings by pressing Control+Alt+S and navigate to Languages & Frameworks | Rust | Rustfmt.

  2. Set the Run rustfmt on Save checkbox.

  3. Click OK to apply the change.

Configure Rustfmt calls

Open settings by pressing Control+Alt+S and navigate to Languages & Frameworks | Rust | Rustfmt.

Rustfmt as default formatter

  • If necessary, specify additional arguments for Rustfmt. All available options are listed in Rustfmt docs. Make sure to add --unstable-features if you use unstable options.

  • Select the preferred Rustfmt channel (stable, beta, nightly, or dev) or let the IDE use the default one.

  • Set custom environment variables if needed.

  • If you want to make Rustfmt the default formatter, select Use rustfmt instead of built-in formatter. For more information, refer to Enable (or disable) Rustfmt instead of the built-in formatter.

  • Set the Run rustfmt on Save checkbox if you want RustRover to reformat Rust files automatically whenever it saves changes. For more information, refer to Automatically run Rustfmt on Save.

Click OK to apply the changes

Formatting with Rustfmt

You can run Rustfmt manually – for a particular file or Cargo project.

Reformat a file with Rustfmt

  1. Open the file you'd like to reformat.

  2. Press Control+Alt+Shift+L or select Code | Reformat File... from the main menu.

  3. In the Reformat File dialog, select the necessary scope. You can also select additional options:

    • Optimize imports: select this option if you want to remove unused imports, add missing ones, or organize import statements.

    • Code cleanup: select this option to run the code cleanup inspections.

    • Do not keep line breaks: reformat line breaks according to the code style settings. This option overrides the Keep when reformatting | Line breaks setting.

    Reformat file dialog
  4. Click Run.

Reformat all files in the current Cargo project

Open the project you'd like to reformat. Do one of the following:

  • Go to Code | Reformat Cargo Project with Rustfmt.

  • Open any Rust file in the editor, right-click, and select Rust | Reformat Cargo Project with Rustfmt to open the context menu.

Last modified: 13 September 2023