rust-lang / rust-lang/rust

Tracking issue for RFC 3681: Default field values

Open
#132,162 67 comments 455 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

B-RFC-implemented B-unstable C-tracking-issue F-default_field_values T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

View all comments

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
Unresolved Questions
  • What is the right interaction wrt. #[non_exhaustive]? Made mutually exclusive.
  • Customization of behavior wrt. visibility rules (allow user to specify that value can be constructed with .. covering defaulted field that otherwise is not accessible) Following RFC: struct can't be constructed with .. if it covers a private field.
  • Allowing .. on types with no default fields, particularly in unit structs?* Disallowed, can be added later if we find a reason to support that.
  • Allowing the use of field_name: _ to specify the use of the default for that specific field?* Let's not try that, particularly in the face of ~const Default allowing for field_name: Default::default() and field_name: default()
  • Tuple structs and tuple variant support* Let file a subsequent RFC for this.
  • Integration with (potential, at this time) structural records*
  • Integration with (potential, at this time) literal type inference (_ { .. })*
  • Exposing default field values as individual consts in the syntax? (I lean towards "unneeded") If an API needs the default to be expressed, it can be an associated const and use field_name: Self::CONST_DEFAULT, making it accessible
  • Expand support to non-const values? (Personal position is that we shouldn't do that, particularly seeing how powerful const eval is becoming.)

* Decision not needed for stabilization of this feature, can be follow up work.

Implementation history

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the unchecked checklist items and their linked Reference, The Book, Rust By Example, and Style Guide work. Read the rustc-dev-guide documentation and stabilization instructions before choosing a subtask. Done means completing a selected documentation or formatting item and reflecting its status in the tracking issue; stabilization remains a separate step.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, documentation
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.