Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the struct update syntax work on non-exhaustive structs?

Tags:

rust

In the struct update syntax, the "spreaded" struct must be the same type as the resulting struct. So the spreaded struct has to contain all fields already.

What, then, is left that is not "exhausted"? Why is the struct update syntax not allowed for non-exhaustive struct?

use some_crate::NonExhaustiveStruct;

let a = NonExhaustiveStruct::default();

let b = {
    some_field: true,
    ..a //Why doesn't this work?
};
like image 732
wisha Avatar asked May 21 '26 02:05

wisha


1 Answers

This is currently an explicitly unsupported edge case: https://rust-lang.github.io/rfcs/2008-non-exhaustive.html#functional-record-updates. Within the same crate struct spread update syntax is allowed on non-exhausive structs but it is not allowed when the struct is defined in a separate crate.

The reasoning for this is that a private field could be added in future, and code outside the crate can't do spread updates of structs with private fields.

like image 197
user1937198 Avatar answered May 25 '26 18:05

user1937198



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!