Rust Reference ยท Operators and Expressions
Struct update syntax
Reference for Rust struct update syntax: building a struct from another with ..base, moves versus copies, the ..Default::default() idiom and privacy limits.
Struct update syntax lets a struct expression take the fields you do not mention from another value of the same type: Config { verbose: true, ..base }. You list the fields that differ, then .. followed by an expression that supplies the rest. It must come last in the field list and has no trailing comma.
The fields copied from the base are moved (or copied, for Copy types) out of it. If any moved field is not Copy, the base value is partially moved afterwards, and you can only use its remaining fields. The most common use is ..Default::default(), which gives you an "options struct" where callers set only what they care about.
let custom = Settings { width: 800, ..Settings::default() };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
StructName {
field_a: value_a,
field_b: value_b,
..base_expression
}
| Part | Description |
|---|---|
field: value |
Fields you set explicitly (shorthand field works when a variable has the same name). |
..base_expression |
An expression of the same struct type. Every field you did not list is taken from it. |
Returns: a new struct value.
Errors: E0063 is the "missing field" error you get without the ..base; E0382 appears when you use base or a moved field after the update; E0451 if a field you rely on is private.
Usage notes
- Only unlisted fields move. Overriding every non-
Copyfield (as withbob) leaves the base completely usable; leaving one non-Copyfield out moves it and makes the base partially moved. - Clone to keep the base:
..base.clone()builds a full clone first, simple but it may copy data you then override. - Same type required:
..basemust have exactly the struct's type. Changing a generic parameter between base and result (Pair<A>toPair<B>) has been discussed for a long time but is not stable. - Privacy: every field that is taken from the base must be visible at the place of the update. A struct with private fields from another module cannot be updated with
..there, even if you only name public fields; use a builder instead. - Works for enum struct variants? No. The syntax is for structs (and, with numeric field names, tuple structs:
Pt { 0: 1, ..other }), not for enum variants. - Order of evaluation: the listed field expressions run first in the order written, then the base expression. The base is evaluated even if you override all fields.
- Not a
Defaultrequirement. Any value of the right type works as the base, including a constant or a function result. - Alternatives: builder types (see type-state and builders) scale better when there are many options or validation rules; struct update is perfect for small config structs and tests.
Related
- struct
- impl
- Range expressions (the other use of
..) - Tutorial: Structs, Copy and Clone, Ownership