-
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
partially initialized structs in local variables, similar to partially moved structs, perhaps should be possible #448
Comments
What happens if I panic between making the struct and fully initializing it? |
The same thing as in the partially moved case. |
Also, the above example won't work anyway, since you could move |
If I simulate an uninitialized state by turning |
I believe @comex is correct and there is nothing semantically wrong with this. |
Now we have MaybeUninit, which covers these use cases. Maybe this can be closed now. @Centril |
OP is about doing so safely, while MaybeUninit is doing so unsafely. MaybeUninit certainly works, but having the language recognize that the use cases shown are safe would be much better. |
Here are two examples where currently a dummy value for a struct field is needed, but the compiler could allow it to be safely left uninitialized by noticing that it's always initialized before being used externally. This is quite analogous to partially moved structs, where the compiler currently allows moving out fields as long as you put them back before using the struct externally (even if the latter is in a loop, as in the second example); but in this case the struct would start in a "partially moved" state.
Even though I think this is a fairly unusual use case (at least, as long as it doesn't allow calling methods in the partially uninitialized state), the added semantic complexity is pretty minimal, so I think it's worth considering.
The text was updated successfully, but these errors were encountered: