Skip to content

Default values for struct fields #1594

@nrc

Description

@nrc

strawman syntax:

struct Foo {
    a: String,
    b: i32 = 42,
}

To instantiate:

let f = Foo { a: "Hello".to_owned() };  // b gets default value of 42
let f = Foo { a: "Hello".to_owned(), .. };  // alternative syntax to make default use explicit
let f = Foo { a: "Hello".to_owned(), b: 51 };  // b is 'overridden'

cc https://internals.rust-lang.org/t/struct-field-defaults/3412

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions