RustRover 2024.2 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.

Run Rustfmt on commit

You can reformat your code automatically with Rustfmt when you commit your changes to the repository if your project is under version control.

  1. Press Alt+0 to open the Commit tool window and click Show Commit Options the Settings button.

  2. Under the Commit Checks menu, enable the Run rustfmt option.

After you have enabled the option, RustRover will run Rustfmt for each file you commit. The setting will remain enabled for the current project as long as you do not disable it.

Advanced commit options popup

Learn more about the actions you can perform on commit from Commit options.

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.

  • Open the Cargo tool window (View | Tool Windows | Cargo) and click the Reform Cargo Project with Rustfmt icon in the toolbar.

Last modified: 08 October 2024