Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute rustfmt on file save in IntelliJ or CLion

How do I run rustfmt automatically when I save a file in IntelliJ or CLion?

like image 865
bit2pixel Avatar asked Dec 28 '19 15:12

bit2pixel


People also ask

Is CLion good for Rust?

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 IntelliJ Rust use rust analyzer?

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.

Which JetBrains IDE for Rust?

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.


2 Answers

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.

enter image description here

like image 166
woodm1979 Avatar answered Sep 25 '22 00:09

woodm1979


  1. 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.

  2. Go to Preferences -> Tools -> File Watchers.

    File Watchers

  3. Add a new watcher and name it rustfmt.

    File Watchers

  4. 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, type which rustfmt in your terminal and copy the path. If you still can't find it, make sure you followed the instructions in Installing Rust.

enter image description here

like image 38
bit2pixel Avatar answered Sep 24 '22 00:09

bit2pixel