Rustfmt
Rustfmt is a formatting tool that enforces the standard Rust-community code style.
tip
A toolchain usually comes with Rustfmt included. If it is missing, and you have Rustup installed, RustRover will offer a one-click installation. You can also add the
rustfmt
component manually.
Since version 2024.3, RustRover uses Rustfmt to format Rust files and projects by default. Code fragments, groups of files, and directories are still formatted by the built-in IDE formatter. If you want to switch back to using built-in formatter for files and projects, refer to the corresponding section.
Open settings by pressing CtrlAlt0S and navigate to Rust | Rustfmt.
To enable Rustfmt, set the Use Rustfmt instead of built-in formatter checkbox. To disable Rustfmt, clear the checkbox.
note
Note that, when Rustfmt is enabled, RustRover will only use it for formatting whole files and Cargo projects. For code fragments, groups of files, and directories, RustRover will automatically switch to the built-in formatter.
Click OK to apply the changes.
You can configure RustRover to run Rustfmt for Rust files whenever they are saved:
Open settings by pressing CtrlAlt0S and navigate to Rust | Rustfmt. .
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.
You can reformat your code automatically with Rustfmt when you commit your changes to the repository if your project is under version control.
note
note
As Rustfmt only formats whole files, the modified code will be formatted together with the containing file(s).
Press Alt00 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.
![Advanced commit options popup Advanced commit options popup](https://resources.jetbrains.com/help/img/idea/2024.3/ri_VCS_advanced_commit_options.png)
Learn more about the actions you can perform on commit from Commit options.
Open settings by pressing CtrlAlt0S and navigate to Rust | Rustfmt. .
![Rustfmt as default formatter Rustfmt as default formatter](https://resources.jetbrains.com/help/img/idea/2024.3/rust_rustfmt_settings.png)
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.tip
To pass options from the command line, use the
--config [key1=val1,key2=val2...]
option.Alternatively, you can list
rustfmt
options in a separate file (rustfmt.toml
or.rustfmt.toml
). For more information, refer to Rustfmt docs.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
You can run Rustfmt manually – for a particular file or Cargo project.
note
Make sure Rustfmt is enabled. Otherwise, RustRover will default to the built-in formatter.
Open the file you'd like to reformat.
Press CtrlAltShift0L or select Code | Reformat File... from the main menu.
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.
Click Run.
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
in the toolbar.
Thanks for your feedback!