Description
openedon Oct 25, 2024
This is a tracking issue for the RFC "3681" (rust-lang/rfcs#3681).
The feature gate for the issue is #![feature(default_field_values)]
.
Allow struct
definitions to provide default values for individual fields and
thereby allowing those to be omitted from initializers. When deriving Default
,
the provided values will then be used. For example:
#[derive(Default)]
struct Pet {
name: Option<String>, // impl Default for Pet will use Default::default() for name
age: i128 = 42, // impl Default for Pet will use the literal 42 for age
}
let valid = Pet { name: None, .. };
assert_eq!(valid.age, 42);
let default = Pet::default();
assert_eq!(default.age, 42);
let invalid = Pet { .. };
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.
Steps
- Implement the RFC (cc @rust-lang/compiler)
- Implement the feature
- Implement lints described in RFC
- Adjust documentation (see instructions on rustc-dev-guide)
- Formatting for new syntax has been added to the Style Guide (nightly-style-procedure)
- Stabilization PR (see instructions on rustc-dev-guide)
Unresolved Questions
XXX --- list all the "unresolved questions" found in the RFC to ensure they are
not forgotten
Implementation history
- Initial implementation: Introduce
default_field_values
feature #129514