Default ergonomics

If one has a struct that contains a member that does not derive Default, but has many members that do, is there some way to default-initialize all of its Default members?

enum NameOrId {
  Id(u64),
  Name(String)
}

struct Foo {
  noi: NameOrId, // There's no default,
  thing1: Option<u64>,
  // ..
  thing99: Option<String>
}

Ideally:

Foo {
  noi: NameOrId::Id(42),
  ..default()
}

We could obviously just split Foo up:

#[derive(Default)]
struct FooDefaultable {
  thing1: Option<u64>,
  // ..
  thing99: Option<String>
}
struct Foo {
  noi: NameOrId, // There's no default,
  defaultable: FooDefaultable
}

I realized over past few days that we have quite a few big explicit struct initialization because only a single member doesn’t/can’t implement Default.

This feature may be of interest Tracking issue for RFC 3681: Default field values · Issue #132162 · rust-lang/rust · GitHub

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.