RustRover 2024.1 Help

Rustfmt

Rustfmt is a formatting tool that enforces the standard Rust-community code style.

By default, RustRover does not use Rustfmt – the built-in IDE formatter is used instead. You can enable Rustfmt, in which case the IDE will use Rustfmt to format Rust files and projects. For code fragments, groups of files, and directories, the IDE will continue using the built-in formatter.

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

  1. Open settings by pressing Ctrl+Alt+S and navigate to Rust | Rustfmt.

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

    Enabling rustfmt instead of the build-in formatter
  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 Ctrl+Alt+S and navigate to Rust | Rustfmt. .

  2. Make sure the Use Rustfmt instead of the built-in formatter checkbox is set.

  3. Click the Configure actions on save link.

    Enabling rustfmt on save
  4. Set the Reformat code checkbox.

    Enabling reformatting on save
  5. Click OK to apply the changes.

Configure Rustfmt calls

Open settings by pressing Ctrl+Alt+S and navigate to 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.

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 Ctrl+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:

  • In the main menu, 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: 20 March 2024