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
An expression may have two roles: it always produces a *value*, and it may have *effects* (otherwise known as "side effects").
51
+
52
+
r[expr.evaluation]
47
53
An expression *evaluates to* a value, and has effects during *evaluation*.
54
+
55
+
r[expr.operands]
48
56
Many expressions contain sub-expressions, called the *operands* of the expression.
57
+
58
+
r[expr.behaviour]
49
59
The meaning of each kind of expression dictates several things:
50
60
51
61
* Whether or not to evaluate the operands when evaluating the expression
52
62
* The order in which to evaluate the operands
53
63
* How to combine the operands' values to obtain the value of the expression
54
64
65
+
r[expr.structure]
55
66
In this way, the structure of expressions dictates the structure of execution.
56
67
Blocks are just another kind of expression, so blocks, statements, expressions, and blocks again can recursively nest inside each other to an arbitrary depth.
57
68
58
69
> **Note**: We give names to the operands of expressions so that we may discuss them, but these names are not stable and may be changed.
59
70
60
71
## Expression precedence
61
72
73
+
r[expr.precedence]
74
+
62
75
The precedence of Rust operators and expressions is ordered as follows, going from strong to weak.
63
76
Binary Operators at the same precedence level are grouped in the order given by their associativity.
64
77
@@ -86,6 +99,9 @@ Binary Operators at the same precedence level are grouped in the order given by
86
99
87
100
## Evaluation order of operands
88
101
102
+
r[expr.operand-order]
103
+
104
+
r[expr.operand-order.default]
89
105
The following list of expressions all evaluate their operands the same way, as described after the list.
90
106
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages.
91
107
@@ -109,6 +125,7 @@ Other expressions either don't take operands or evaluate them conditionally as d
109
125
* Range expression
110
126
* Return expression
111
127
128
+
r[expr.operand-order.operands-before-primary]
112
129
The operands of these expressions are evaluated prior to applying the effects of the expression.
113
130
Expressions taking multiple operands are evaluated left to right as written in the source code.
114
131
@@ -132,17 +149,27 @@ assert_eq!(
132
149
133
150
## Place Expressions and Value Expressions
134
151
152
+
r[expr.place-value]
153
+
154
+
r[expr.place-value.intro]
135
155
Expressions are divided into two main categories: place expressions and value expressions;
136
156
there is also a third, minor category of expressions called assignee expressions.
137
157
Within each expression, operands may likewise occur in either place context or value context.
138
158
The evaluation of an expression depends both on its own category and the context it occurs within.
139
159
160
+
r[expr.place-value.place-memory-location]
140
161
A *place expression* is an expression that represents a memory location.
162
+
163
+
r[expr.place-value.place-expr-kinds]
141
164
These expressions are [paths] which refer to local variables, [static variables], [dereferences][deref] (`*expr`), [array indexing] expressions (`expr[expr]`), [field] references (`expr.f`) and parenthesized place expressions.
165
+
166
+
r[expr.place-value.value-expr-kinds]
142
167
All other expressions are value expressions.
143
168
169
+
r[expr.place-value.value-result]
144
170
A *value expression* is an expression that represents an actual value.
145
171
172
+
r[expr.place-value.place-context]
146
173
The following contexts are *place expression* contexts:
147
174
148
175
* The left operand of a [compound assignment] expression.
@@ -157,6 +184,7 @@ The following contexts are *place expression* contexts:
157
184
158
185
> Note: Historically, place expressions were called *lvalues* and value expressions were called *rvalues*.
159
186
187
+
r[expr.place-value.assignee]
160
188
An *assignee expression* is an expression that appears in the left operand of an [assignment][assign] expression.
161
189
Explicitly, the assignee expressions are:
162
190
@@ -169,29 +197,46 @@ Explicitly, the assignee expressions are:
169
197
fields).
170
198
-[Unit structs][_StructExpression_].
171
199
200
+
r[expr.place-value.parentehesis]
172
201
Arbitrary parenthesisation is permitted inside assignee expressions.
173
202
174
203
### Moved and copied types
175
204
205
+
r[expr.move]
206
+
207
+
r[expr.move.intro]
176
208
When a place expression is evaluated in a value expression context, or is bound by value in a pattern, it denotes the value held _in_ that memory location.
209
+
210
+
r[expr.move.copy]
177
211
If the type of that value implements [`Copy`], then the value will be copied.
212
+
213
+
r[expr.move.requires-sized]
178
214
In the remaining situations, if that type is [`Sized`], then it may be possible to move the value.
215
+
216
+
r[expr.move.movable-place]
179
217
Only the following place expressions may be moved out of:
180
218
181
219
*[Variables] which are not currently borrowed.
182
220
*[Temporary values](#temporaries).
183
221
*[Fields][field] of a place expression which can be moved out of and don't implement [`Drop`].
184
222
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and that can also be moved out of.
185
223
224
+
r[expr.move.deinitialization]
186
225
After moving out of a place expression that evaluates to a local variable, the location is deinitialized and cannot be read from again until it is reinitialized.
226
+
227
+
r[expr.move.place-invalid]
187
228
In all other cases, trying to use a place expression in a value expression context is an error.
188
229
189
230
### Mutability
190
231
232
+
r[expr.mut]
233
+
234
+
r[expr.mut.intro]
191
235
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow], [implicitly mutably borrowed], or bound to a pattern containing `ref mut`, it must be _mutable_.
192
236
We call these *mutable place expressions*.
193
237
In contrast, other place expressions are called *immutable place expressions*.
194
238
239
+
r[expr.mut.valid-places]
195
240
The following expressions can be mutable place expression contexts:
196
241
197
242
* Mutable [variables] which are not currently borrowed.
@@ -208,12 +253,17 @@ The following expressions can be mutable place expression contexts:
208
253
209
254
### Temporaries
210
255
256
+
r[expr.temporary]
257
+
211
258
When using a value expression in most place expression contexts, a temporary unnamed memory location is created and initialized to that value.
212
259
The expression evaluates to that location instead, except if [promoted] to a `static`.
213
260
The [drop scope] of the temporary is usually the end of the enclosing statement.
214
261
215
262
### Implicit Borrows
216
263
264
+
r[expr.implicit-borrow]
265
+
266
+
r[expr.implicit-borrow-intro]
217
267
Certain expressions will treat an expression as a place expression by implicitly borrowing it.
218
268
For example, it is possible to compare two unsized [slices][slice] for equality directly, because the `==` operator implicitly borrows its operands:
219
269
@@ -230,6 +280,7 @@ let b: &[i32];
230
280
::std::cmp::PartialEq::eq(&*a, &*b);
231
281
```
232
282
283
+
r[expr.implicit-borrow.application]
233
284
Implicit borrows may be taken in the following expressions:
234
285
235
286
* Left operand in [method-call] expressions.
@@ -242,23 +293,28 @@ Implicit borrows may be taken in the following expressions:
242
293
243
294
## Overloading Traits
244
295
296
+
r[expr.overload]
297
+
245
298
Many of the following operators and expressions can also be overloaded for other types using traits in `std::ops` or `std::cmp`.
246
299
These traits also exist in `core::ops` and `core::cmp` with the same names.
247
300
248
301
## Expression Attributes
249
302
303
+
r[expr.attr]
304
+
305
+
r[expr.attr.restriction]
250
306
[Outer attributes][_OuterAttribute_] before an expression are allowed only in a few specific cases:
251
307
252
308
* Before an expression used as a [statement].
253
309
* Elements of [array expressions], [tuple expressions], [call expressions], and tuple-style [struct] expressions.
0 commit comments