@@ -15,6 +15,35 @@ In this way, the structure of expressions dictates the structure of execution.
15
15
Blocks are just another kind of expression, so blocks, statements, expressions,
16
16
and blocks again can recursively nest inside each other to an arbitrary depth.
17
17
18
+ ## Expression precedence
19
+
20
+ The precedence of Rust operators and expressions is ordered as follows, going
21
+ from strong to weak. Binary Operators at the same precedence level are
22
+ evaluated in the order given by their associativity.
23
+
24
+ | Operator/Expression | Associativity |
25
+ | -----------------------------| ---------------------|
26
+ | Paths | |
27
+ | Method calls | |
28
+ | Field expressions | left to right |
29
+ | Function calls, array indexing | |
30
+ | ` ? ` | |
31
+ | Unary ` - ` ` * ` ` ! ` ` & ` ` &mut ` | |
32
+ | ` as ` ` : ` | left to right |
33
+ | ` * ` ` / ` ` % ` | left to right |
34
+ | ` + ` ` - ` | left to right |
35
+ | ` << ` ` >> ` | left to right |
36
+ | ` & ` | left to right |
37
+ | ` ^ ` | left to right |
38
+ | <code >| ; </code > | left to right |
39
+ | ` == ` ` != ` ` < ` ` > ` ` <= ` ` >= ` | Require parentheses |
40
+ | ` && ` | left to right |
41
+ | <code >| ;| ; </code > | left to right |
42
+ | ` .. ` ` ... ` | Require parentheses |
43
+ | ` <- ` | right to left |
44
+ | ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` <br > ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
45
+ | ` return ` ` break ` closures | |
46
+
18
47
## Lvalues and rvalues
19
48
20
49
Expressions are divided into two main categories: _ lvalues_ and _ rvalues_ .
@@ -23,25 +52,24 @@ or _rvalue context_. The evaluation of an expression depends both on its own
23
52
category and the context it occurs within.
24
53
25
54
An lvalue is an expression that represents a memory location. These expressions
26
- are paths which refer to local variables, function and method arguments, or
27
- static variables, [ dereferences]   ; (` *expr ` ), [ array indexing] expressions
28
- (` expr[expr] ` ), [ field] references (` expr.f ` ) and parenthesized lvalue
29
- expressions. All other expressions are rvalues.
55
+ are [ paths] ( #path-expressions ) which refer to local variables, static
56
+ variables, function parameters, [ dereferences]   ; (` *expr ` ), [ array indexing]
57
+ expressions (` expr[expr] ` ), [ field] references (` expr.f ` ) and parenthesized
58
+ lvalue expressions. All other expressions are rvalues.
30
59
31
60
The left operand of an [ assign] ment or [ compound assignment] expression is an
32
61
lvalue context, as is the single operand of a unary [ borrow] , and the operand
33
62
of any [ implicit borrow] ( #implicit-borrows ) . The discriminant or subject of a
34
- [ match] expression and right side of a ` let ` binding may be an lvalue context,
35
- if ref bindings are made, but is otherwise an rvalue context. All other
36
- expression contexts are rvalue contexts.
63
+ [ match] expression and right side of a ` let ` is also an lvalue context. All
64
+ other expression contexts are rvalue contexts.
37
65
38
66
### Moved and copied types
39
67
40
- When an lvalue is evaluated in an _ rvalue context_ , it denotes the value held
41
- _ in_ that memory location. If value is of a type that implements ` Copy ` , then
42
- the value will be copied. In the remaining situations if the type of the value
43
- is [ ` Sized ` ] ( the-sized-trait.html ) it may be possible to move the value. Only
44
- the following lvalues may be moved out of:
68
+ When an lvalue is evaluated in an _ rvalue context_ or is bound by value in a
69
+ pattern, it denotes the value held _ in_ that memory location. If value is of a
70
+ type that implements ` Copy ` , then the value will be copied. In the remaining
71
+ situations if the type of the value is [ ` Sized ` ] ( the-sized-trait.html ) it may
72
+ be possible to move the value. Only the following lvalues may be moved out of:
45
73
46
74
* [ Variables] ( variables.html ) which are not currently borrowed.
47
75
* [ Temporary values] ( #temporary-lifetimes ) .
@@ -88,11 +116,11 @@ That is, the promoted expression can be evaluated at compile-time and the
88
116
resulting value does not contain interior mutability or destructors (these
89
117
properties are determined based on the value where possible, e.g. ` &None `
90
118
always has the type ` &'static Option<_> ` , as it contains nothing disallowed).
91
- Otherwise, the lifetime of temporary values is typically
119
+ Otherwise, the lifetime of temporary values is typically
92
120
93
- - the innermost enclosing statement; the tail expression of a block is
121
+ - the innermost enclosing statement; the tail expression of a block is
94
122
considered part of the statement that encloses the block, or
95
- - the condition expression or the loop conditional expression if the
123
+ - the condition expression or the loop conditional expression if the
96
124
temporary is created in the condition expression of an ` if ` or an ` if ` /` else `
97
125
or in the loop conditional expression of a ` while ` expression.
98
126
@@ -119,7 +147,7 @@ Here are some examples:
119
147
an rvalue. As the temporary is created in the condition expression
120
148
of an ` if ` /` else ` , it will be freed at the end of the condition expression
121
149
(in this example before the call to ` bar ` or ` baz ` is made).
122
- - ` let x = if temp().must_run_bar {bar()} else {baz()}; ` .
150
+ - ` let x = if temp().must_run_bar {bar()} else {baz()}; ` .
123
151
Here we assume the type of ` temp() ` is a struct with a boolean field
124
152
` must_run_bar ` . As the previous example, the temporary corresponding to
125
153
` temp() ` will be freed at the end of the condition expression.
@@ -186,29 +214,25 @@ also constant expressions:
186
214
* [ Literals] .
187
215
* [ Paths] to [ functions] ( items/functions.html ) and constants.
188
216
Recursively defining constants is not allowed.
189
- * Paths to statics, so long as only their address, not their value, is used.
190
- This includes using their value indirectly through a complicated expression.
191
- \*
192
217
* [ Tuple expressions] .
193
218
* [ Array expressions] .
194
219
* [ Struct] expressions, where the type does not implement [ ` Drop ` ] ( the-drop-trait.html ) .
195
220
* [ Enum variant] expressions, where the enumeration type does not implement ` Drop ` .
196
- * [ Block expressions]   ; (and ` unsafe ` blocks) which contain only items and
221
+ * [ Block expressions]   ; (and ` unsafe ` blocks) which only contain items and
197
222
possibly a (constant) tail expression.
198
223
* [ Field] expressions.
199
224
* Index expressions, [ array indexing] or [ slice] with a ` usize ` .
200
225
* [ Range expressions] .
201
226
* [ Closure expressions] which don't capture variables from the environment.
202
227
* Built in [ negation] , [ arithmetic, logical] , [ comparison] or [ lazy boolean]
203
228
operators used on integer and floating point types, ` bool ` and ` char ` .
204
- * Shared [ borrow] .
205
- * The [ dereference operator] , but not to circumvent the rule on statics.
229
+ * Shared [ borrow] , except if applied to a type with [ interior
230
+ mutability] ( interior-mutability.html ) .
231
+ * The [ dereference operator] .
206
232
* [ Grouped] expressions.
207
233
* [ Cast] expressions, except pointer to address and
208
234
function pointer to address casts.
209
235
210
- \* Only in static items.
211
-
212
236
## Overloading Traits
213
237
214
238
Many of the following operators and expressions can also be overloaded for
0 commit comments