@@ -44,117 +44,125 @@ evaluated in the order given by their associativity.
44
44
| ` = ` ` += ` ` -= ` ` *= ` ` /= ` ` %= ` <br > ` &= ` <code >| ; =</code > ` ^= ` ` <<= ` ` >>= ` | right to left |
45
45
| ` return ` ` break ` closures | |
46
46
47
- ## Lvalues and rvalues
47
+ ## Place Expressions and Value Expressions
48
48
49
- Expressions are divided into two main categories: _ lvalues _ and _ rvalues _ .
50
- Likewise within each expression, sub-expressions may occur in _ lvalue context _
51
- or _ rvalue context _ . The evaluation of an expression depends both on its own
52
- category and the context it occurs within.
49
+ Expressions are divided into two main categories: place expressions and
50
+ value expressions. Likewise within each expression, sub-expressions may occur
51
+ in either place context or value context . The evaluation of an expression
52
+ depends both on its own category and the context it occurs within.
53
53
54
- An lvalue is an expression that represents a memory location. These expressions
55
- are [ paths] ( expressions/path-expr.html ) 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 .
54
+ A * place expression * is an expression that represents a memory location. These
55
+ expressions are [ paths] which refer to local variables, [ static variables ] ,
56
+ [ dereferences]   ; (` *expr ` ), [ array indexing] expressions ( ` expr[expr] ` ),
57
+ [ field] references (` expr.f ` ) and parenthesized place expressions. All other
58
+ expressions are value expressions.
59
59
60
- The left operand of an [ assign] ment or [ compound assignment] expression is an
61
- lvalue context, as is the single operand of a unary [ borrow] , and the operand
62
- of any [ implicit borrow] ( #implicit-borrows ) . The discriminant or subject of a
63
- [ match] expression and right side of a ` let ` is also an lvalue context. All
64
- other expression contexts are rvalue contexts.
60
+ A * value expression* is an expression that represents an actual value.
61
+
62
+ The left operand of an [ assignment] [ assign ] or [ compound assignment] expression
63
+ is a place expression context, as is the single operand of a unary [ borrow] , and
64
+ the operand of any [ implicit borrow] . The discriminant or subject of a
65
+ [ match expression] [ match ] and right side of a [ let statement] is also a place
66
+ expression context. All other expression contexts are value expression contexts.
67
+
68
+ > Note: Historically, place expressions were called * lvalues* and value
69
+ > expressions were called * rvalues* .
65
70
66
71
### Moved and copied types
67
72
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:
73
+ When a place expression is evaluated in a value expression context, or is bound
74
+ by value in a pattern, it denotes the value held _ in_ that memory location. If
75
+ the type of that value implements ` Copy ` , then the value will be copied. In the
76
+ remaining situations if that type is [ ` Sized ` ] ( the-sized-trait.html ) , then it
77
+ may be possible to move the value. Only the following place expressions may be
78
+ moved out of:
73
79
74
- * [ Variables] ( variables.html ) which are not currently borrowed.
80
+ * [ Variables] which are not currently borrowed.
75
81
* [ Temporary values] ( #temporary-lifetimes ) .
76
- * [ Field ] s of an lvalue which can be moved out of and
82
+ * [ Fields ] [ field ] of a place expression which can be moved out of and
77
83
doesn't implement [ ` Drop ` ] ( the-drop-trait.html ) .
78
- * The result of [ dereferencing] an expression with type ` Box<T> ` and that can
84
+ * The result of [ dereferencing] an expression with type [ ` Box<T> ` ] and that can
79
85
also be moved out of.
80
86
81
- Moving out of an lvalue deinitializes that location (if it comes from a local
82
- variable), so that it can't be read from again. In all other cases, trying to
83
- use an lvalue in an rvalue context is an error.
87
+ Moving out of a place expression that evaluates to a local variable, the
88
+ location is deinitialized and cannot be read from again until it is
89
+ reinitialized. In all other cases, trying to use a place expression in a value
90
+ expression context is an error.
84
91
85
92
### Mutability
86
93
87
- For an lvalue to be [ assign] ed to, mutably [ borrow] ed,
88
- [ implicitly mutably borrowed] ( #implicit-borrows )
89
- or bound to a pattern containing ` ref mut ` it must be _ mutable_ , we call these
90
- contexts _ mutable_ lvalue contexts, other lvalue contexts are called
91
- _ immutable_ .
94
+ For a place expression to be [ assigned] [ assign ] to, mutably [ borrowed] [ borrow ] ,
95
+ [ implicitly mutably borrowed] , or bound to a pattern containing ` ref mut ` it
96
+ must be _ mutable_ . We call these * mutable place expressions* . In contrast,
97
+ other place expressions are called * immutable place expressions* .
92
98
93
- The following expressions can create mutable lvalues :
99
+ The following expressions can be mutable place expression contexts :
94
100
95
- * Mutable [ variables] ( variables.html ) , which are not currently borrowed.
96
- * [ Mutable ` static ` items] ( items/static-items.html#mutable-statics ) .
97
- * [ Temporary values] ( #temporary-lifetimes ) .
98
- * [ Field] s, this evaluates the subexpression in a mutable lvalue context.
101
+ * Mutable [ variables] , which are not currently borrowed.
102
+ * [ Mutable ` static ` items] .
103
+ * [ Temporary values] .
104
+ * [ Fields] [ field ] , this evaluates the subexpression in a mutable place
105
+ expression context.
99
106
* [ Dereferences] of a ` *mut T ` pointer.
100
107
* Dereference of a variable, or field of a variable, with type ` &mut T ` . Note:
101
- this is an exception to the requirement for the next rule.
108
+ This is an exception to the requirement of the next rule.
102
109
* Dereferences of a type that implements ` DerefMut ` , this then requires that
103
- the value being dereferenced is evaluated is a mutable lvalue context.
110
+ the value being dereferenced is evaluated is a mutable place expression context.
104
111
* [ Array indexing] of a type that implements ` DerefMut ` , this
105
- then evaluates the value being indexed ( but not the index) in mutable lvalue
106
- context.
112
+ then evaluates the value being indexed, but not the index, in mutable place
113
+ expression context.
107
114
108
115
### Temporary lifetimes
109
116
110
- When using an rvalue in most lvalue contexts, a temporary unnamed lvalue is
111
- created and used instead, if not promoted to ` 'static ` . Promotion of an
112
- rvalue expression to a ` 'static ` slot occurs when the expression could be
117
+ When using a value expression in most place expression contexts, a temporary
118
+ unnamed memory location is created initialized to that value and the expression
119
+ evaluates to that location instead, except if promoted to ` 'static ` . Promotion
120
+ of a value expression to a ` 'static ` slot occurs when the expression could be
113
121
written in a constant, borrowed, and dereferencing that borrow where the
114
122
expression was the originally written, without changing the runtime behavior.
115
123
That is, the promoted expression can be evaluated at compile-time and the
116
- resulting value does not contain interior mutability or destructors (these
124
+ resulting value does not contain [ interior mutability] or [ destructors] (these
117
125
properties are determined based on the value where possible, e.g. ` &None `
118
126
always has the type ` &'static Option<_> ` , as it contains nothing disallowed).
119
127
Otherwise, the lifetime of temporary values is typically
120
128
121
129
- the innermost enclosing statement; the tail expression of a block is
122
130
considered part of the statement that encloses the block, or
123
131
- the condition expression or the loop conditional expression if the
124
- temporary is created in the condition expression of an ` if ` or an ` if ` / ` else `
125
- or in the loop conditional expression of a ` while ` expression.
132
+ temporary is created in the condition expression of an ` if ` or in the loop
133
+ conditional expression of a ` while ` expression.
126
134
127
- When a temporary rvalue is being created that is assigned into a ` let `
128
- declaration, however, the temporary is created with the lifetime of the
129
- enclosing block instead, as using the enclosing statement (the ` let `
130
- declaration) would be a guaranteed error (since a pointer to the temporary
135
+ When a temporary value expression is being created that is assigned into a
136
+ [ ` let ` declaration] [ let ] , however, the temporary is created with the lifetime of
137
+ the enclosing block instead, as using the enclosing [ ` let ` declaration ] [ let ]
138
+ would be a guaranteed error (since a pointer to the temporary
131
139
would be stored into a variable, but the temporary would be freed before the
132
140
variable could be used). The compiler uses simple syntactic rules to decide
133
141
which values are being assigned into a ` let ` binding, and therefore deserve a
134
142
longer temporary lifetime.
135
143
136
144
Here are some examples:
137
145
138
- - ` let x = foo(&temp()) ` . The expression ` temp() ` is an rvalue . As it
146
+ - ` let x = foo(&temp()) ` . The expression ` temp() ` is a value expression . As it
139
147
is being borrowed, a temporary is created which will be freed after
140
- the innermost enclosing statement ( the ` let ` declaration, in this case) .
148
+ the innermost enclosing statement; in this case, the ` let ` declaration.
141
149
- ` let x = temp().foo() ` . This is the same as the previous example,
142
150
except that the value of ` temp() ` is being borrowed via autoref on a
143
151
method-call. Here we are assuming that ` foo() ` is an ` &self ` method
144
152
defined in some trait, say ` Foo ` . In other words, the expression
145
153
` temp().foo() ` is equivalent to ` Foo::foo(&temp()) ` .
146
154
- ` let x = if foo(&temp()) {bar()} else {baz()}; ` . The expression ` temp() ` is
147
- an rvalue . As the temporary is created in the condition expression
148
- of an ` if ` / ` else ` , it will be freed at the end of the condition expression
149
- ( in this example before the call to ` bar ` or ` baz ` is made) .
155
+ a value expression . As the temporary is created in the condition expression
156
+ of an ` if ` , it will be freed at the end of the condition expression;
157
+ in this example before the call to ` bar ` or ` baz ` is made.
150
158
- ` let x = if temp().must_run_bar {bar()} else {baz()}; ` .
151
159
Here we assume the type of ` temp() ` is a struct with a boolean field
152
160
` must_run_bar ` . As the previous example, the temporary corresponding to
153
161
` temp() ` will be freed at the end of the condition expression.
154
162
- ` while foo(&temp()) {bar();} ` . The temporary containing the return value from
155
163
the call to ` temp() ` is created in the loop conditional expression. Hence it
156
- will be freed at the end of the loop conditional expression ( in this example
157
- before the call to ` bar ` if the loop body is executed) .
164
+ will be freed at the end of the loop conditional expression; in this example
165
+ before the call to ` bar ` if the loop body is executed.
158
166
- ` let x = &temp() ` . Here, the same temporary is being assigned into
159
167
` x ` , rather than being passed as a parameter, and hence the
160
168
temporary's lifetime is considered to be the enclosing block.
@@ -164,12 +172,13 @@ Here are some examples:
164
172
- ` let x = [ &temp() ] ` . As in the previous case, the
165
173
temporary is assigned into an array which is then assigned into a
166
174
binding, and hence it is given the lifetime of the enclosing block.
167
- - ` let ref x = temp() ` . In this case, the temporary is created using a ref binding,
168
- but the result is the same: the lifetime is extended to the enclosing block.
175
+ - ` let ref x = temp() ` . In this case, the temporary is created using a ref
176
+ binding, but the result is the same: the lifetime is extended to the enclosing
177
+ block.
169
178
170
179
### Implicit Borrows
171
180
172
- Certain expressions will treat an expression as an lvalue by implicitly
181
+ Certain expressions will treat an expression as a place expression by implicitly
173
182
borrowing it. For example, it is possible to compare two unsized [ slices] for
174
183
equality directly, because the ` == ` operator implicitly borrows it's operands:
175
184
@@ -192,7 +201,7 @@ Implicit borrows may be taken in the following expressions:
192
201
* Left operand in [ field] expressions.
193
202
* Left operand in [ call expressions] .
194
203
* Left operand in [ array indexing] expressions.
195
- * Operand of the [ dereference] operator (` * ` ).
204
+ * Operand of the [ dereference operator] \ (` * ` ).
196
205
* Operands of [ comparison] .
197
206
* Left operands of the [ compound assignment] .
198
207
@@ -236,7 +245,7 @@ to be ran.
236
245
## Overloading Traits
237
246
238
247
Many of the following operators and expressions can also be overloaded for
239
- other types using traits in ` std::ops ` or ` std::cmp ` , these traits here also
248
+ other types using traits in ` std::ops ` or ` std::cmp ` . These traits also
240
249
exist in ` core::ops ` and ` core::cmp ` with the same names.
241
250
242
251
[ block expressions ] : expressions/block-expr.html
@@ -270,5 +279,14 @@ exist in `core::ops` and `core::cmp` with the same names.
270
279
[ overflow ] : expressions/operator-expr.html#overflow
271
280
272
281
[ destructors ] : destructors.html
273
- [ interior-mutability ] : interior-mutability.html
282
+ [ interior mutability ] : interior-mutability.html
283
+ [ `Box<T>` ] : ../std/boxed/struct.Box.html
284
+ [ implicit borrow ] : #implicit-borrows
285
+ [ implicitly mutably borrowed ] : #implicit-borrows
286
+ [ let ] : statements.html#let-statements
287
+ [ let statement ] : statements.html#let-statements
288
+ [ Mutable `static` items ] : items/static-items.html#mutable-statics
274
289
[ slice ] : types.html#array-and-slice-types
290
+ [ static variables ] : items/static-items.html
291
+ [ Temporary values ] : #temporary-lifetimes
292
+ [ Variables ] : variables.html
0 commit comments