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
Open settings by pressing Ctrl+Alt+S and navigate to
.To enable Rustfmt, set the Use Rustfmt instead of built-in formatter checkbox. To disable Rustfmt, clear the checkbox.
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:
Open settings by pressing Ctrl+Alt+S and navigate to
. .Make sure the Use Rustfmt instead of the built-in formatter checkbox is set.
Click the Configure actions on save link.
Set the Reformat code checkbox.
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.
Press Alt+0 to open the Commit tool window and click Show Commit Options .
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.
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
. .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
Open the file you'd like to reformat.
Press Ctrl+Alt+Shift+L or select
from the main menu.In the
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 setting.
Click
.
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
.Open any Rust file in the editor, right-click, and select
to open the context menu.Open the Cargo tool window ( ) and click in the toolbar.