rustfmt is a program used for formatting Rust programs.
Questions tagged [rustfmt]
22 questions
30
votes
2 answers
How can I switch off rustfmt for a region of code instead of a single item?
#[rustfmt::skip] allows you to skip a "block" of code while formatting, but this requires putting skip on each {} instead of Clang-style on/off
Consider this code:
fn add(a : i32, b : i32) -> i32 { a + b }
fn sub(a : i32, b : i32) -> i32 { a - b…

skgbanga
- 2,477
- 2
- 20
- 29
18
votes
2 answers
Is there a stable way to tell Rustfmt to skip an entire file
The official way to get Rustfmt to skip an item is #[rustfmt::skip], however I want it to skip an entire file. I tried this:
#![rustfmt::skip]
However you get this error
error[E0658]: non-builtin inner attributes are unstable
Here is the issue…

Timmmm
- 88,195
- 71
- 364
- 509
9
votes
2 answers
Execute rustfmt on file save in IntelliJ or CLion
How do I run rustfmt automatically when I save a file in IntelliJ or CLion?

bit2pixel
- 1,122
- 8
- 8
7
votes
5 answers
How to pretty print Syn AST?
I'm trying to use syn to create an AST from a Rust file and then using quote to write it to another. However, when I write it, it puts extra spaces between everything.
Note that the example below is just to demonstrate the minimum reproducible…

Mr.Smithyyy
- 2,157
- 12
- 49
- 95
5
votes
1 answer
Why do I get 'unstable features are only available in nightly channel' when running rustfmt?
I have just updated my Rust to rustc 1.63.0 (4b91a6ea7 2022-08-08)
In my .rustfmt.toml file
# Basic
hard_tabs = true
max_width = 100
use_small_heuristics = "Max"
# Imports
imports_granularity = "Crate"
reorder_imports = true
#…

Russo
- 2,186
- 2
- 26
- 42
5
votes
2 answers
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 =…

Aritra Dattagupta
- 760
- 1
- 7
- 22
4
votes
1 answer
Is it possible to use rustfmt to format just the imports?
I've been messing around with nightly rustfmt and all its options (so many more than the stable version).
So, just out of curiosity: if I want to use rustfmt only to format my imports (the use statements), without touching the rest of the code, is…

rodrigocfd
- 6,450
- 6
- 34
- 68
3
votes
0 answers
Rust fmt configuration for vertical chaining
The following is what I desire to format to:
async fn main() {
let req = reqwest::get("http://wttr.in/brisbane")
.await
.expect("Failed to get weather data")
.text()
.await;
}
This is what I get (unless it does…

ripbozo
- 45
- 4
3
votes
1 answer
How can I format a string like this?
static TEST: &str = "test: {}";
fn main() {
let string = format!(TEST, "OK");
println!("{}", string);
}
I want to construct the string "test: OK", but this doesn't work. How can I do it?

Fiono
- 133
- 4
2
votes
1 answer
Does Rustfmt have an option to make types explicit?
Some IDEs can help with type visualization. Here is an example from VS Code (a slightly modified example from the The Rust Programming Language book):
But other viewers (for instance, used in PR reviews) don't.
Is it possible to setup Rustfmt in…

ZakiMa
- 5,637
- 1
- 24
- 48
2
votes
1 answer
Pretty printing data structures in a more compact format?
I'm exploring ways that I can nicely format const tables of data to be more compact and easier to read. For example, I have [[u64; 64]; 2] where rustfmt just spaghettifies into hundreds of lines. It would be nice to format into something more…
user1002430
2
votes
1 answer
Format multi-line string literal arguments without extra lines
I would like to call macros with a single multi-line string argument, formatted like so:
css!(r"
background: grey;
color: white;
");
However, Rustfmt insists on putting the string literal on its own line, which is ugly and takes up more…

Reinis Mazeiks
- 1,168
- 1
- 10
- 21
2
votes
1 answer
How can I auto-format Rust (and C++) code on commit automatically?
I would like to automatically format the code when I do commit using rustfmt the same way as I did it before for clang-format -i. I.e. format only the lines of code which has been updated in the commit without touching other code. How to do it?

Kirill Lykov
- 1,293
- 2
- 22
- 39
2
votes
1 answer
Use nightly rustfmt with stable compiler in CLion
I am running into an unexpected issue when it comes to toolchains in CLion. I want to use the stable channel for the compiler while using the nightly for rustfmt. The reason is that I want to use rustfmt features that are not available in the stable…

Christian Ivicevic
- 10,071
- 7
- 39
- 74
2
votes
1 answer
Unknown configuration option `group_imports` for rustfmt
I have a rustfmt.toml file with the line group_imports = "StdExternalCrate" in it, which according to the documentation should be a valid option. However, I get the following warning:
Warning: Unknown configuration option `group_imports`
Running…

Ted Klein Bergman
- 9,146
- 4
- 29
- 50