-
Notifications
You must be signed in to change notification settings - Fork 996
Description
Summary
I tried to format this code:
fn main() {
// case 1
x = X { field1, field2, xx };
let X { field1, field2, .. } = x;
// case 2
x = X { field1, field2x, xx };
let X { field1, field2x, .. } = x;
// case 3
x = X { field1, field2, field3, xx };
let X { field1, field2, field3, .. } = x;
}Expected behavior
I expected each struct literal or pattern to either be kept on a single line, or formatted vertically (with each field on a separate line).
Furthermore, within each of the three cases, I expected the struct literal and struct pattern to be formatted the same way, because they had the same width in the input.
Actual behavior
Instead, Rustfmt (with default options) formats the code like this:
fn main() {
// case 1
x = X { field1, field2, xx };
let X { field1, field2, .. } = x;
// case 2
x = X {
field1,
field2x,
xx,
};
let X {
field1, field2x, ..
} = x;
// case 3
x = X {
field1,
field2,
field3,
xx,
};
let X {
field1,
field2,
field3,
..
} = x;
}Cases 1 and 3 look okay, but case 2 is unexpected. The struct literal in the first line was formatted vertically, but the struct pattern in the second line was formatted neither vertically nor on a single line: the struct is split across multiple lines, but all the fields are on the same line.
Configuration
Default configuration (missing or blank rustfmt.toml).
Reproduction Steps
Format the code at the beginning of this issue with default options.
Meta
rustfmt --version:
rustfmt 1.9.0-nightly (3b1b0ef4d8 2026-03-11)