Skip to content

Proposed formatting for default field values #6449

Closed
@estebank

Description

@estebank
Contributor

Proposed addition to https://github.com/rust-lang/rust/blob/master/src/doc/style-guide/src/items.md#structs-and-unions to account for default field values:


If the struct has a const default field value, if the value is a single expression
it should be on the same line.

struct Foo {
    a: A,
    b: B = const_b(),
}

If the const value is a const block, it should follow the same rules as the
value of const items, matching the field's indentation.

struct Foo {
    a: A,
    b: i32 = const {
        const_val().const_method() + CONST_VAL
    },
}

If the identifier, type and const value do not fit within the right margin, move
the default value down one line first and indent it one step from the previous
line, following the rules of assignment operators in expressions.

struct Foo {
    a: A,
    b: B,
    long_name: LongType =
        long_const_val(),
}

If the identifier and type still don't fit within the right margin, pull the type
down and indent it one level. If the type and const value fit within the right
margin, keep them in a single line, otherwise pull the default value down one line
and indent it one step further.

struct Foo {
    a: A,
    b: B,
    long_name:
        LongType = const_val(),
}
union Foo {
    a: A,
    b: B,
    long_name:
        LongType =
            long_const_val(),
}

Activity

ytmimi

ytmimi commented on Jan 16, 2025

@ytmimi
Contributor

I think this should be moved to the style-teams repo

estebank

estebank commented on Jan 16, 2025

@estebank
ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @estebank@ytmimi

        Issue actions

          Proposed formatting for default field values · Issue #6449 · rust-lang/rustfmt