Search code examples
visual-studio-coderustrustfmt

VSCode Rust add semicolon on save


I am using the Rust extension on vscode and NOT rust-analyzer. However, when I am saving a file, vscode is using rustfmt to format my file but it doesn't automatically insert semicolons. I have a trivial function like this

fn call_me() {
  let x = 5
  println!(x)
}

It doesn't add the necessary semicolons. How do I make it add semicolons? Are my installations somehow messed up?

Also, I have tried rust-analyzer and it doesn't add semicolons either.


Solution

  • Unlike JavaScript, semicolons are not syntactically optional in Rust. Thus, leaving them out is a syntax error, not just a matter of style, and rustfmt (the standard Rust code formatting tool) doesn't ever attempt to fix any syntax errors, no matter how “obvious” they might be — if it reads a file with errors it will not make any formatting changes.

    (I don't know if there's a way to get rust-analyzer, vim, or VS Code to auto-insert semicolons as a matter of editing rather than formatting.)