How do I run rustfmt
automatically when I save a file in IntelliJ or CLion?
Rust is supported in CLion via the IntelliJ Rust plugin, compatible with all IntelliJ-based IDEs. The plugin provides Rust language engine with a set of code insight features, Cargo support, and debugger functionality (in CLion and IntelliJ IDEA Ultimate).
Does the plugin use RLS, Racer, or Rust Analyzer? The plugin's language analysis is implemented from scratch, leveraging the IntelliJ Platform infrastructure for incremental analysis and indexing. However, IntelliJ Rust shares some code for procedural macros expansion with rust-analyzer.
IntelliJ Rust brings JetBrains-quality language support and the full IDE experience to your Rust workflow. Rust becomes native to IntelliJ IDEA, CLion, and other IntelliJ-based IDEs with smart coding assistance, seamless Cargo support, built-in test runner, and code coverage tooling.
It should be noted that while bit2pixel's answer will work, (and indeed is how I did it for a good long while), there's a newer (better?) way. The rust-plugin has an option to handle this automatically.
Make sure you have File Watchers
plugin via Preferences -> Plugins -> Installed
. If it's not installed, go to the Marketplace
tab and search for File Watchers
, install it, and restart your IDE.
Go to Preferences -> Tools -> File Watchers
.
Add a new watcher and name it rustfmt
.
Configure the watcher by following the screenshot below. Enter the path to rustfmt
into the Program
field; it can be found in your Cargo bin directory
.
If you don't know where
Cargo bin directory
directory is, typewhich rustfmt
in your terminal and copy the path. If you still can't find it, make sure you followed the instructions in Installing Rust.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With