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