rustrustfmt

Is there a rustfmt option for placing opening brace on a new line when assigning block value to a variable?


I want the following code:

let x = {
    let y = 10;
    5
};

to be formatted as this:

let x =
{
    let y = 10;
    5
};

I like when opening and closing braces are always on the same vertical line, no matter what use case is.

It seems that these parameters in rustfmt.toml don't affect the code:

unstable_features = true
brace_style = "AlwaysNextLine"
control_brace_style = "AlwaysNextLine"

Nevertheless, blocks after if and function bodies are being formatted as expected.


Solution

  • As of Rustfmt version 1.8.0, the brace_style option does not apply to block expressions (and many other uses of curly braces in the Rust syntax), so the formatting you want is not possible. See the relevant tracking issue on the Rustfmt GitHub repo, as well as this related issue, where a Rustfmt developer says:

    brace_style as originally introduced only applied to items and not every possible syntactical construct where braces may occur. [...] suffice to say it's currently expected that the option doesn't apply in certain contexts.