You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/range-expr.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,8 @@
1
1
# Range expressions
2
2
3
+
r[expr.range]
4
+
5
+
r[expr.range.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _RangeExpression_ :\
5
8
> _RangeExpr_\
@@ -27,6 +30,7 @@
27
30
> _RangeToInclusiveExpr_ :\
28
31
> `..=`[_Expression_]
29
32
33
+
r[expr.range.behaviour]
30
34
The `..` and `..=` operators will construct an object of one of the `std::ops::Range` (or `core::ops::Range`) variants, according to the following table:
Copy file name to clipboardExpand all lines: src/expressions/return-expr.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,16 @@
1
1
# `return` expressions
2
2
3
+
r[expr.return]
4
+
5
+
r[expr.return.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _ReturnExpression_ :\
5
8
> `return`[_Expression_]<sup>?</sup>
6
9
10
+
r[expr.return.intro]
7
11
Return expressions are denoted with the keyword `return`.
12
+
13
+
r[expr.return.behaviour]
8
14
Evaluating a `return` expression moves its argument into the designated output location for the current function call, destroys the current function activation frame, and transfers control to the caller frame.
Copy file name to clipboardExpand all lines: src/expressions/struct-expr.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,8 @@
1
1
# Struct expressions
2
2
3
+
r[expr.struct]
4
+
5
+
r[expr.struct.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _StructExpression_ :\
5
8
> _StructExprStruct_\
@@ -29,6 +32,7 @@
29
32
>
30
33
> _StructExprUnit_ : [_PathInExpression_]
31
34
35
+
r[expr.struct.intro]
32
36
A *struct expression* creates a struct, enum, or union value.
33
37
It consists of a path to a [struct], [enum variant], or [union] item followed by the values for the fields of the item.
34
38
There are three forms of struct expressions: struct, tuple, and unit.
@@ -51,17 +55,29 @@ some_fn::<Cookie>(Cookie);
51
55
52
56
## Field struct expression
53
57
58
+
r[expr.struct.field]
59
+
60
+
r[expr.struct.field.intro]
54
61
A struct expression with fields enclosed in curly braces allows you to specify the value for each individual field in any order.
55
62
The field name is separated from its value with a colon.
56
63
64
+
r[expr.struct.field.union-constraint]
57
65
A value of a [union] type can only be created using this syntax, and it must specify exactly one field.
58
66
59
67
## Functional update syntax
60
68
69
+
r[expr.struct.update]
70
+
71
+
r[expr.struct.update.intro]
61
72
A struct expression that constructs a value of a struct type can terminate with the syntax `..` followed by an expression to denote a functional update.
73
+
74
+
r[expr.struct.update.constraint]
62
75
The expression following `..` (the base) must have the same struct type as the new struct type being formed.
63
76
77
+
r[expr.struct.update.fields]
64
78
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
79
+
80
+
r[expr.struct.update.visibility-constraint]
65
81
As with all struct expressions, all of the fields of the struct must be [visible], even those not explicitly named.
66
82
67
83
```rust
@@ -72,9 +88,11 @@ Point3d {y: 0, z: 10, .. base}; // OK, only base.x is accessed
72
88
drop(y_ref);
73
89
```
74
90
91
+
r[expr.struct.restriction]
75
92
Struct expressions with curly braces can't be used directly in a [loop] or [if] expression's head, or in the [scrutinee] of an [if let] or [match] expression.
76
93
However, struct expressions can be used in these situations if they are within another expression, for example inside [parentheses].
77
94
95
+
r[expr.struct.tuple-field]
78
96
The field names can be decimal integer values to specify indices for constructing tuple structs.
79
97
This can be used with base structs to fill out the remaining indices not specified:
80
98
@@ -87,6 +105,8 @@ let c3 = Color{1: 0, ..c2}; // Fill out all other fields using a base struct.
87
105
88
106
### Struct field init shorthand
89
107
108
+
r[expr.struct.field.named]
109
+
90
110
When initializing a data structure (struct, enum, union) with named (but not numbered) fields, it is allowed to write `fieldname` as a shorthand for `fieldname: fieldname`.
91
111
This allows a compact syntax with less duplication.
92
112
For example:
@@ -102,6 +122,8 @@ Point3d { x, y: y_value, z };
102
122
103
123
## Tuple struct expression
104
124
125
+
r[expr.struct.tuple]
126
+
105
127
A struct expression with fields enclosed in parentheses constructs a tuple struct.
106
128
Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's constructor. For example:
107
129
@@ -114,6 +136,8 @@ let pos = c(8, 6, 7); // Creates a `Position` value.
114
136
115
137
## Unit struct expression
116
138
139
+
r[expr.struct.unit]
140
+
117
141
A unit struct expression is just the path to a unit struct item.
118
142
This refers to the unit struct's implicit constant of its value.
119
143
The unit struct value can also be constructed with a fieldless struct expression. For example:
A *tuple expression* constructs [tuple values][tuple type].
13
19
20
+
r[expr.tuple.intro]
14
21
The syntax for tuple expressions is a parenthesized, comma separated list of expressions, called the *tuple initializer operands*.
22
+
23
+
r[expr.tuple.unary-tuple-restriction]
15
24
1-ary tuple expressions require a comma after their tuple initializer operand to be disambiguated with a [parenthetical expression].
16
25
26
+
r[expr.tuple.value]
17
27
Tuple expressions are a [value expression] that evaluate into a newly constructed value of a tuple type.
28
+
29
+
r[expr.tuple.type]
18
30
The number of tuple initializer operands is the arity of the constructed tuple.
31
+
32
+
r[expr.tuple.unit]
19
33
Tuple expressions without any tuple initializer operands produce the unit tuple.
34
+
35
+
r[expr.tuple.fields]
20
36
For other tuple expressions, the first written tuple initializer operand initializes the field `0` and subsequent operands initializes the next highest field.
21
37
For example, in the tuple expression `('a', 'b', 'c')`, `'a'` initializes the value of the field `0`, `'b'` field `1`, and `'c'` field `2`.
22
38
@@ -31,19 +47,27 @@ Examples of tuple expressions and their types:
31
47
32
48
## Tuple indexing expressions
33
49
50
+
r[expr.tuple-index]
51
+
52
+
r[expr.tuple-index.syntax]
34
53
> **<sup>Syntax</sup>**\
35
54
> _TupleIndexingExpression_ :\
36
55
> [_Expression_]`.`[TUPLE_INDEX]
37
56
57
+
r[expr.tuple-index.intro]
38
58
A *tuple indexing expression* accesses fields of [tuples][tuple type] and [tuple structs][tuple struct].
39
59
40
60
The syntax for a tuple index expression is an expression, called the *tuple operand*, then a `.`, then finally a tuple index.
61
+
62
+
r[expr.tuple-index.restriction]
41
63
The syntax for the *tuple index* is a [decimal literal] with no leading zeros, underscores, or suffix.
42
64
For example `0` and `2` are valid tuple indices but not `01`, `0_`, nor `0i32`.
43
65
66
+
r[expr.tuple-index.constraint]
44
67
The type of the tuple operand must be a [tuple type] or a [tuple struct].
45
68
The tuple index must be a name of a field of the type of the tuple operand.
46
69
70
+
r[expr.tuple-index.result]
47
71
Evaluation of tuple index expressions has no side effects beyond evaluation of its tuple operand.
48
72
As a [place expression], it evaluates to the location of the field of the tuple operand with the same name as the tuple index.
0 commit comments