This is something that confuses me - I thought that
let x = Hello {
name: "Link",
..y
}
was pretty much equivalent to
let mut x = y;
x.name = "Link";
let x = x;
. But then why can't I use the .. syntax with structs that have both private and public fields, changing only the public fields? This might be useful if I was going to, for example, build a builder-like struct, using a private field to keep the struct extensible (kind of like how people use a hidden __Nonexhaustive variant to keep enums extensible).
If part of a structure is private then outside the module you must use a public function to construct since your code does not know the private detail.
Your second code snippet isn't constructing a new instance only modifying. (And moving to a different variable identifier.)
Interestingly, it's called struct update syntax in the book, which implies that it's not the same as just constructing a new instance. Anyway, isn't the fomer snippet just sugar for the latter?
let x = Hello {
name: "Link",
something: y.something,
something_else: y.something_else,
}
with the compiler just going through and listing out all the fields you haven't explicitly given a value to. It's only really an "update" syntax if you're thinking about immutable data structures, where to update part of a value you just construct a new instance with that part changed.
Builders are typically implemented with methods exposing the configurable parts, and then you can hide fields and/or other private state the normal way. You don't generally need struct update syntax there because the builder methods move the self value in and out: