The Default macro is a compiler builtin. Currently we can #[derive(Default)] for structs but not for enums. I wonder if somebody else would like to have the ability to #[derive(Default)] for enums in std.
Through when #[derive(Default)] for structs, struct fields can only be
the default value of the field type and cannot be customized with optional value.
What I am proposing is to add the #[default] attribute to enums.
The enum variant with #[default] label cannot be customized like structs fields.
Field-less enums:
#[derive(Default)]
pub enum E {
#[default]
A,
B,
}
assert_eq!(E::default(), E::A);
Struct field enums:
#[derive(Default)]
pub enum E {
A,
#[default]
B {
a: i32,
b: String
}
}
assert_eq!(E::default(), E::B { a: 0, b: "".to_string() });
Thanks. I've already known how to implement the Default trait. The manually code
to implement Default is not too long. But I find the inconsistence that
structs can be #[derive(Default)] but enums cannot.