-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
Correct the partial initialization of a struct #42692
Comments
Inferring from your post, I think you are missing a key fact: The So the current syntax always requires a completely constructed object. What is proposed here, initialising missing fields individually by some method, would be an entirely new feature I think (and thus https://github.com/rust-lang/rfcs may be a better place for this issue). |
Yes, this change would need an RFC. If you'd like to pursue this, please go through the RFC process. You can start by either filing an issue on rust-lang/rfcs or starting a thread on internals.rust-lang.org to discuss this. |
@Mark-Simulacrum, @TimNN okay, I will make an rfc. |
@TimNN Could you point me do the docs which explain current behavior please?
|
@vityafx: See either the second edition of the Book, the first edition of the Book or the Reference. |
@TimNN Thanks! Sorry for asking a question again, could you also point me to the source code where this syntax is parsed? I am working on "Implementation details" part now of the rfc.. |
@vityafx: No problem! The parsing happens here as far as I can tell, although you shouldn't need to look at that code to write the RFC. |
@TimNN yes, I understand that :) I just want to implement the feature by myself and try some variants of it. Thanks a lot for the link! |
I believe @ubsan was recently asking about this as well; maybe they'd be interested in getting involved |
not me, sorry! The only thing I'm really interested in is allowing |
I workaround this by having a |
Default values for a struct may be different from default values of any individual struct member. #[derive(Debug)]
struct A {
one: i32,
two: i32,
three: i32,
}
impl Default for A {
fn default() -> Self {
A {
one: 1,
two: 2,
three: 3,
}
}
}
dbg!(A {
one: 111,
..default() // expecting `two: 2`, not `two: 0`
}); Thus, you cannot initialize struct with default values of each struct members, that would be ambiguous and/or break backward compatibility. |
Yes, it is ambiguous and would require breaking backwards compatibility. That's why I am not so sure anymore whether it is good to have it, or we could just live with it. |
@vityafx Did you ever get around to writing an RFC for this? |
Can't remember, probably not. |
Here is a question about the partial initialization of a struct. The answer there says that to use the
..Default::default()
syntax your type must implement theDefault
trait, however, I don't see any reason for this.Assume we have the following code:
So, when we want to partially initialize it we actually don't need a
Default
trait to be implemented for our structB
, but we do for it's fields which we don't initialize (we initialize them by usingDefault
):So, there is really no need in
Default
trait to be implement for the whole structureB
if it's fields do that. You simply don't even touch theDefault
trait ofB
structure, but you do that for it's fields!Can we improve partial structs initialization not to require
Default
trait for the whole type if we don't use it actually?The text was updated successfully, but these errors were encountered: