|
| 1 | +# `default_field_values` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#132162] |
| 4 | + |
| 5 | +[#132162]: https://github.com/rust-lang/rust/issues/132162 |
| 6 | + |
| 7 | +------------------------ |
| 8 | + |
| 9 | +The `default_field_values` feature allows users to specify a const value for |
| 10 | +individual fields in struct definitions, allowing those to be omitted from |
| 11 | +initializers. |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | +```rust |
| 16 | +#![feature(default_field_values)] |
| 17 | + |
| 18 | +#[derive(Default)] |
| 19 | +struct Pet { |
| 20 | + name: Option<String>, // impl Default for Pet will use Default::default() for name |
| 21 | + age: i128 = 42, // impl Default for Pet will use the literal 42 for age |
| 22 | +} |
| 23 | + |
| 24 | +fn main() { |
| 25 | + let a = Pet { name: Some(String::new()), .. }; // Pet { name: Some(""), age: 42 } |
| 26 | + let b = Pet::default(); // Pet { name: None, age: 42 } |
| 27 | + assert_eq!(a.age, b.age); |
| 28 | + // The following would be a compilation error: `name` needs to be specified |
| 29 | + // let _ = Pet { .. }; |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## `#[derive(Default)]` |
| 34 | + |
| 35 | +When deriving Default, the provided values are then used. On enum variants, |
| 36 | +the variant must still be marked with `#[default]` and have all its fields |
| 37 | +with default values. |
| 38 | + |
| 39 | +```rust |
| 40 | +#![feature(default_field_values)] |
| 41 | + |
| 42 | +#[derive(Default)] |
| 43 | +enum A { |
| 44 | + #[default] |
| 45 | + B { |
| 46 | + x: i32 = 0, |
| 47 | + y: i32 = 0, |
| 48 | + } |
| 49 | + C, |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Enum variants |
| 54 | + |
| 55 | +This feature also supports enum variants for both specifying default values |
| 56 | +and `#[derive(Default)]`. |
| 57 | + |
| 58 | +## Interaction with `#[non_exhaustive]` |
| 59 | + |
| 60 | +A struct or enum variant marked with `#[non_exhaustive]` is not allowed to |
| 61 | +have default field values. |
| 62 | + |
| 63 | +## Lints |
| 64 | + |
| 65 | +When manually implementing the `Default` trait for a type that has default |
| 66 | +field values, if any of these are overriden in the impl the |
| 67 | +`default_overrides_default_fields` lint will trigger. This lint is in place |
| 68 | +to avoid surprising diverging behavior between `S { .. }` and |
| 69 | +`S::default()`, where using the same type in both ways could result in |
| 70 | +different values. The appropriate way to write a manual `Default` |
| 71 | +implementation is to use the functional update syntax: |
| 72 | + |
| 73 | +```rust |
| 74 | +#![feature(default_field_values)] |
| 75 | + |
| 76 | +struct Pet { |
| 77 | + name: String, |
| 78 | + age: i128 = 42, // impl Default for Pet will use the literal 42 for age |
| 79 | +} |
| 80 | + |
| 81 | +impl Default for Pet { |
| 82 | + fn default() -> Pet { |
| 83 | + Pet { |
| 84 | + name: "no-name".to_string(), |
| 85 | + .. |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
0 commit comments