How to run cargo fmt on save in vscode?
Asked Answered
A

7

48

Is it possible to make Visual Studio Code run cargo fmt on file save?

Admass answered 6/6, 2021 at 14:0 Comment(0)
T
92

Install the extension rust-analyzer (the officially recommended vscode extension), and add the following to settings.json:

"[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer",
    "editor.formatOnSave": true
}
Thelma answered 18/5, 2022 at 1:2 Comment(0)
J
29
  1. Install rust-analyzer, if you have not already.
  2. In Visual Studio Code's settings, enable Editor: Format On Save (editor.formatOnSave).
Jeanene answered 6/6, 2021 at 16:57 Comment(1)
I'll also add that you should ensure you don't have "editor.defaultFormatter" set globally. If you have, say, "editor.defaultFormatter": "esbenp.prettier-vscode" (or some other formatter) enabled globally rather than on a per-language basis, VSCode will attempt to use that formatter in lieu of rust-analyzer's on Rust code. I've seen a few JS devs lost 10 or 15 minutes to this when transitioning to a Rust project.Magda
M
22

This is what worked for me. In the file settings.json somewhere inside the curly braces insert the following :

    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "rust-analyzer.rustfmt.enableRangeFormatting": true,
    "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust-analyzer", 
        "editor.formatOnSave": true 
    },
Morbidity answered 22/12, 2021 at 12:21 Comment(0)
N
15

If you have formatOnSaveMode is set to modifications or modificationsIfAvailable, you may need to change it to file. This is probably a small issue with the analyzer.

And if you want to leave the default settings unchanged, do it for only rust;

"[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer",
    "editor.formatOnSave": true,
    "editor.formatOnSaveMode": "file"
  },
Nominal answered 11/12, 2022 at 18:57 Comment(2)
i just need to add: "editor.formatOnSaveMode": "file" it's the working answer! thank you!Lengthy
This was it for me, thanks! With modifications set it silently did nothing.Lapidate
B
9

Current version of the Rust extension > 0.7.8 requires nothing else to be installed. Enable formatOnSave in VS Code settings.json file:

  "[rust]": {
        "editor.formatOnSave": true
    }
Backer answered 30/12, 2021 at 3:54 Comment(0)
A
0

Posting on behalf of @Stacks. Here's a more complete set of steps that will identify any problems with steps 1-2. Just enabling "format on save" may not always be enough:

  1. Install rust-analyzer
  2. Enable Editor: Format On Save (as in the other answers)
  3. Test Formatting your code by manually selecting a section of code and either right clicking “Format Code..” or pressing the “Format Code” shortcut (Cntrl-Shift-F or Alt-Shift-F)
  4. Follow any prompts to select a different formatter if necessary.

The last steps 3-4 are important, because if your formatting preferences are incorrect or if there are duplicate formatters installed, it will fail and prompt you to select the correct one to use.

Acute answered 27/5, 2024 at 19:0 Comment(0)
P
-2

Install rust-analyzer and add this to your settings.json

"[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer",
    "editor.formatOnSave": true
}
Pitchblack answered 4/10, 2022 at 7:34 Comment(1)
This is a duplicate of Delta Kapp's answer above.Ethel

© 2022 - 2025 — McMap. All rights reserved.