#209
Jun 2026
209. ..Default::default() — set the fields you care about, default the rest
Building a struct with a dozen fields when you only want to change one is tedious. Struct update syntax lets you fill in the rest from a default — or from any other value.
Say you have a config struct. Spelling out every field just to flip one flag is noise:
| |
Derive Default, then use ..Default::default() to fill the fields you didn’t mention:
| |
The ..base part isn’t limited to Default — you can spread from any existing instance, so it doubles as a cheap “clone but tweak”:
| |
Two things to remember: the ..rest has to come last, and the fields it pulls in are moved out of the source value (or copied, if they’re Copy) — so a non-Copy field like host means you can’t keep using base afterward.