Rust formatter moves code into one line

Hi, Rust formatter (in VS Code) moves code into one line even if it shouldn't. For example the guessing game from the Rust book

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

is formatted into a single line

    io::stdin().read_line(&mut guess).expect("Failed to read line");

Is it possible to get the first formatting by default?

Didn't actually test it, but I assume you could use a sufficiently small value for chain_width?

Create a rustfmt.toml in your project's directory and add the chain_width parameter in it.

Thanks, this works. I set chain_width to 20 just for testing and it worked. Maybe I have to tweak it a little bit up or down

You can also use inline comments as a method of forcing lines to stay separate:

io::stdin() // describe something here
        .read_line(&mut guess) // or leave blank, these are fairly self-explanatory
        .expect("Failed to read line");
1 Like

If you don't want to change project-wide settings, I believe that rustfmt will respect annotations, so you could do something like this:

#[rustfmt::skip]
io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.