]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/expressions/struct-expr.md
New upstream version 1.30.0+dfsg1
[rustc.git] / src / doc / reference / src / expressions / struct-expr.md
CommitLineData
ea8adc8c
XL
1# Struct expressions
2
3There are several forms of struct expressions. A _struct expression_ consists
ff7c6d11 4of the [path] of a [struct item](items/structs.html), followed by a
ea8adc8c
XL
5brace-enclosed list of zero or more comma-separated name-value pairs, providing
6the field values of a new instance of the struct. A field name can be any
ff7c6d11 7[identifier], and is separated from its value expression by a
ea8adc8c
XL
8colon. In the case of a tuple struct the field names are numbers corresponding
9to the position of the field. The numbers must be written in decimal,
10containing no underscores and with no leading zeros or integer suffix. A value
11of a [union](items/unions.html) type can also be created using this syntax,
12except that it must specify exactly one field.
13
8faf50e0 14Struct expressions can't be used directly in the head of a [loop]
ea8adc8c
XL
15or an [if], [if let] or [match] expression. But struct expressions can still be
16in used inside parentheses, for example.
17
ea8adc8c
XL
18A _tuple struct expression_ consists of the path of a struct item, followed by
19a parenthesized list of one or more comma-separated expressions (in other
20words, the path of a struct item followed by a tuple expression). The struct
21item must be a tuple struct item.
22
23A _unit-like struct expression_ consists only of the path of a struct item.
24
25The following are examples of struct expressions:
26
27```rust
28# struct Point { x: f64, y: f64 }
29# struct NothingInMe { }
30# struct TuplePoint(f64, f64);
31# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
32# struct Cookie; fn some_fn<T>(t: T) {}
33Point {x: 10.0, y: 20.0};
34NothingInMe {};
35TuplePoint(10.0, 20.0);
36TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line
37let u = game::User {name: "Joe", age: 35, score: 100_000};
38some_fn::<Cookie>(Cookie);
39```
40
41A struct expression forms a new value of the named struct type. Note that for a
42given *unit-like* struct type, this will always be the same value.
43
44A struct expression can terminate with the syntax `..` followed by an
45expression to denote a functional update. The expression following `..` (the
46base) must have the same struct type as the new struct type being formed. The
47entire expression denotes the result of constructing a new struct (with the
48same type as the base expression) with the given values for the fields that
49were explicitly specified and the values in the base expression for all other
50fields. Just as with all struct expressions, all of the fields of the struct
51must be [visible](visibility-and-privacy.html), even those not explicitly
52named.
53
54```rust
55# struct Point3d { x: i32, y: i32, z: i32 }
56let base = Point3d {x: 1, y: 2, z: 3};
57Point3d {y: 0, z: 10, .. base};
58```
59
60## Struct field init shorthand
61
62When initializing a data structure (struct, enum, union) with named (but not
63numbered) fields, it is allowed to write `fieldname` as a shorthand for
64`fieldname: fieldname`. This allows a compact syntax with less duplication.
65
66Example:
67
68```rust
69# struct Point3d { x: i32, y: i32, z: i32 }
70# let x = 0;
71# let y_value = 0;
72# let z = 0;
73Point3d { x: x, y: y_value, z: z };
74Point3d { x, y: y_value, z };
75```
ff7c6d11
XL
76
77[IDENTIFIER]: identifiers.html
78[path]: paths.html
79[loop]: expressions/loop-expr.html
80[if]: expressions/if-expr.html#if-expressions
81[if let]: expressions/if-expr.html#if-let-expressions
8faf50e0 82[match]: expressions/match-expr.html