#[derive(Default)]
{
// Attributes available to this derive:
#[default]
}
Expand description
Derives Default with more flexibility than Rust’s built-in #[derive(Default)].
In addition to the functionality allowed by the built-in #[derive(Default)], this version
allows:
- overriding the default values for particular fields using
#[default = ...]or#[default(...)]; and - using
#[default]on a non-unit enum variant
§Examples
§Structs
#[derive(derive_extras::Default)]
struct ExampleStruct {
x: i32,
#[default = 15]
y: i32,
z: i32,
}For this struct, the following Default implementation is derived:
impl Default for ExampleStruct {
fn default() -> Self {
Self {
x: Default::default(),
y: 15,
z: Default::default(),
}
}
}§Enums
#[derive(derive_extras::Default)]
enum ExampleEnum {
Unit,
Tuple(i32, i32, i32),
#[default]
Struct {
x: i32,
#[default = 15]
y: i32,
z: i32,
},
}For this enum, the following Default implementation is derived:
impl Default for ExampleEnum {
fn default() -> Self {
Self::Struct {
x: Default::default(),
y: 15,
z: Default::default(),
}
}
}