Skip to content

Struct patterns containing .. are sometimes formatted neither vertically nor on a single line #6827

@taylordotfish

Description

@taylordotfish

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsArea: patternsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEneeds-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions