-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default values for struct fields #1594
Comments
I would much rather introduce MyStruct {
a: 2,
.. // means: .. Default::default()
} |
Would the default be an arbitrary expression, executed at |
At the very least the syntax for using the default should be explicit (IE: using ..), otherwise this feature could become slightly confusing. |
Hi, I think |
There was a bit of discussion on this about a year ago here: https://www.reddit.com/r/rust/comments/2m99ly/idea_for_default_struct_fields/ Probably the most important thing about this proposal is that's it's providing more flexibility than just Default, as you can have default fields for a struct that isn't neccessarily default altogether. |
It would be cool to provide both a const initializer (via default values) and a non-const one (via the Example: // In some library
pub struct Bar {
... // internals
}
// In my code
struct Foo {
i: u64,
b: Bar,
}
static f: Foo = Foo {
i: 34,
b: Default::default(), // Ooops! Cannot do this because `default` is non-const
}; In many cases, this leads to the use of a |
Also useful in a case like:
When later on you want to add a new field:
you don't have to change code across the whole project to add |
@kindlychung I think that mess with the goal of explicitness. |
Whether support private fields? I think like this code
|
Closing in favor of #1806 for tracking purposes. |
Hey what ended up happening to this? Would be super handle for the use case I have right now. Doesnt seam to have make it into code? |
This RFC was closed in favour of 1806, which was closed as postponed, which means the lang team did not want to add the feature to the language at the time. I'm no longer on the lang team, but I believe this is not a current priority. It may be at some point. |
There is a new pre-RFC at IRLO. |
strawman syntax:
To instantiate:
cc https://internals.rust-lang.org/t/struct-field-defaults/3412
The text was updated successfully, but these errors were encountered: