Using two spaces in Rust projects with rustfmt

in rust

I like to use two spaces to indent my code, but Rust’s formatter rustfmt defaults to four.

Generally I follow the language conventions, but this spacing indentation really started annoying me when switching between projects.

Luckily, Rust has an easy way to fix this!

  1. Create a file at the root of your project (or workspace) called rustfmt.toml
  2. In this file, add the line tab_spaces = 2
  3. That’s it!

The next time you save or format your file, rustfmt will pick up the config and use the correct spacing.

For more rustfmt options, either run rustfmt --help=config or have a look at their config page.