Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed formatting for default field values #205

Open
estebank opened this issue Jan 16, 2025 · 0 comments
Open

Proposed formatting for default field values #205

estebank opened this issue Jan 16, 2025 · 0 comments

Comments

@estebank
Copy link

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(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant