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.md
+52-85
Original file line number
Diff line number
Diff line change
@@ -42,28 +42,24 @@
42
42
> | [_MatchExpression_]\
43
43
> )
44
44
45
-
An expression may have two roles: it always produces a *value*, and it may have
46
-
*effects* (otherwise known as "side effects"). An expression *evaluates to* a
47
-
value, and has effects during *evaluation*. Many expressions contain
48
-
sub-expressions, called the *operands* of the expression. The meaning of each
49
-
kind of expression dictates several things:
45
+
An expression may have two roles: it always produces a *value*, and it may have *effects* (otherwise known as "side effects").
46
+
An expression *evaluates to* a value, and has effects during *evaluation*.
47
+
Many expressions contain sub-expressions, called the *operands* of the expression.
48
+
The meaning of each kind of expression dictates several things:
50
49
51
50
* Whether or not to evaluate the operands when evaluating the expression
52
51
* The order in which to evaluate the operands
53
52
* How to combine the operands' values to obtain the value of the expression
54
53
55
54
In this way, the structure of expressions dictates the structure of execution.
56
-
Blocks are just another kind of expression, so blocks, statements, expressions,
57
-
and blocks again can recursively nest inside each other to an arbitrary depth.
55
+
Blocks are just another kind of expression, so blocks, statements, expressions, and blocks again can recursively nest inside each other to an arbitrary depth.
58
56
59
-
> **Note**: We give names to the operands of expressions so that we may discuss
60
-
> them, but these names are not stable and may be changed.
57
+
> **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.
61
58
62
59
## Expression precedence
63
60
64
-
The precedence of Rust operators and expressions is ordered as follows, going
65
-
from strong to weak. Binary Operators at the same precedence level are grouped
66
-
in the order given by their associativity.
61
+
The precedence of Rust operators and expressions is ordered as follows, going from strong to weak.
62
+
Binary Operators at the same precedence level are grouped in the order given by their associativity.
@@ -89,9 +85,8 @@ in the order given by their associativity.
89
85
90
86
## Evaluation order of operands
91
87
92
-
The following list of expressions all evaluate their operands the same way, as
93
-
described after the list. Other expressions either don't take operands or
94
-
evaluate them conditionally as described on their respective pages.
88
+
The following list of expressions all evaluate their operands the same way, as described after the list.
89
+
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages.
95
90
96
91
* Dereference expression
97
92
* Error propagation expression
@@ -113,15 +108,13 @@ evaluate them conditionally as described on their respective pages.
113
108
* Range expression
114
109
* Return expression
115
110
116
-
The operands of these expressions are evaluated prior to applying the effects of
117
-
the expression. Expressions taking multiple operands are evaluated left to right
118
-
as written in the source code.
111
+
The operands of these expressions are evaluated prior to applying the effects of the expression.
112
+
Expressions taking multiple operands are evaluated left to right as written in the source code.
119
113
120
114
> **Note**: Which subexpressions are the operands of an expression is
121
115
> determined by expression precedence as per the previous section.
122
116
123
-
For example, the two `next` method calls will always be called in the same
124
-
order:
117
+
For example, the two `next` method calls will always be called in the same order:
125
118
126
119
```rust
127
120
# // Using vec instead of array to avoid references
@@ -134,23 +127,18 @@ assert_eq!(
134
127
);
135
128
```
136
129
137
-
> **Note**: Since this is applied recursively, these expressions are also
138
-
> evaluated from innermost to outermost, ignoring siblings until there are no
139
-
> inner subexpressions.
130
+
> **Note**: Since this is applied recursively, these expressions are also evaluated from innermost to outermost, ignoring siblings until there are no inner subexpressions.
140
131
141
132
## Place Expressions and Value Expressions
142
133
143
-
Expressions are divided into two main categories: place expressions and value
144
-
expressions; there is also a third, minor category of expressions called
145
-
assignee expressions. Within each expression, operands may likewise occur in
146
-
either place context or value context. The evaluation of an expression depends
147
-
both on its own category and the context it occurs within.
134
+
Expressions are divided into two main categories: place expressions and value expressions;
135
+
there is also a third, minor category of expressions called assignee expressions.
136
+
Within each expression, operands may likewise occur in either place context or value context.
137
+
The evaluation of an expression depends both on its own category and the context it occurs within.
148
138
149
-
A *place expression* is an expression that represents a memory location. These
150
-
expressions are [paths] which refer to local variables, [static variables],
[field] references (`expr.f`) and parenthesized place expressions. All other
153
-
expressions are value expressions.
139
+
A *place expression* is an expression that represents a memory location.
140
+
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.
141
+
All other expressions are value expressions.
154
142
155
143
A *value expression* is an expression that represents an actual value.
156
144
@@ -166,11 +154,10 @@ The following contexts are *place expression* contexts:
166
154
expression.
167
155
* The base of a [functional update] struct expression.
168
156
169
-
> Note: Historically, place expressions were called *lvalues* and value
170
-
> expressions were called *rvalues*.
157
+
> Note: Historically, place expressions were called *lvalues* and value expressions were called *rvalues*.
171
158
172
-
An *assignee expression* is an expression that appears in the left operand of an
173
-
[assignment][assign] expression. Explicitly, the assignee expressions are:
159
+
An *assignee expression* is an expression that appears in the left operand of an[assignment][assign] expression.
When a place expression is evaluated in a value expression context, or is bound
189
-
by value in a pattern, it denotes the value held _in_ that memory location. If
190
-
the type of that value implements [`Copy`], then the value will be copied. In
191
-
the remaining situations, if that type is [`Sized`], then it may be possible to
192
-
move the value. Only the following place expressions may be moved out of:
175
+
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.
176
+
If the type of that value implements [`Copy`], then the value will be copied.
177
+
In the remaining situations, if that type is [`Sized`], then it may be possible to move the value.
178
+
Only the following place expressions may be moved out of:
193
179
194
180
*[Variables] which are not currently borrowed.
195
181
*[Temporary values](#temporaries).
196
-
*[Fields][field] of a place expression which can be moved out of and
197
-
don't implement [`Drop`].
198
-
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and
199
-
that can also be moved out of.
182
+
*[Fields][field] of a place expression which can be moved out of and don't implement [`Drop`].
183
+
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and that can also be moved out of.
200
184
201
-
After moving out of a place expression that evaluates to a local variable, the
202
-
location is deinitialized and cannot be read from again until it is
203
-
reinitialized. In all other cases, trying to use a place expression in a value
204
-
expression context is an error.
185
+
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.
186
+
In all other cases, trying to use a place expression in a value expression context is an error.
205
187
206
188
### Mutability
207
189
208
-
For a place expression to be [assigned][assign] to, mutably [borrowed][borrow],
209
-
[implicitly mutably borrowed], or bound to a pattern containing `ref mut`, it
210
-
must be _mutable_. We call these *mutable place expressions*. In contrast,
211
-
other place expressions are called *immutable place expressions*.
190
+
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_.
191
+
We call these *mutable place expressions*.
192
+
In contrast, other place expressions are called *immutable place expressions*.
212
193
213
194
The following expressions can be mutable place expression contexts:
214
195
215
196
* Mutable [variables] which are not currently borrowed.
216
197
*[Mutable `static` items].
217
198
*[Temporary values].
218
-
*[Fields][field]: this evaluates the subexpression in a mutable place
219
-
expression context.
199
+
*[Fields][field]: this evaluates the subexpression in a mutable place expression context.
220
200
*[Dereferences][deref] of a `*mut T` pointer.
221
-
* Dereference of a variable, or field of a variable, with type `&mut T`. Note:
222
-
This is an exception to the requirement of the next rule.
223
-
* Dereferences of a type that implements `DerefMut`: this then requires that
224
-
the value being dereferenced is evaluated in a mutable place expression context.
225
-
*[Array indexing] of a type that implements `IndexMut`: this
226
-
then evaluates the value being indexed, but not the index, in mutable place
227
-
expression context.
201
+
* Dereference of a variable, or field of a variable, with type `&mut T`.
202
+
Note: This is an exception to the requirement of the next rule.
203
+
* Dereferences of a type that implements `DerefMut`:
204
+
this then requires that the value being dereferenced is evaluated in a mutable place expression context.
205
+
*[Array indexing] of a type that implements `IndexMut`:
206
+
this then evaluates the value being indexed, but not the index, in mutable place expression context.
228
207
229
208
### Temporaries
230
209
231
-
When using a value expression in most place expression contexts, a temporary
232
-
unnamed memory location is created and initialized to that value. The expression
233
-
evaluates to that location instead, except if [promoted] to a `static`. The
234
-
[drop scope] of the temporary is usually the end of the enclosing statement.
210
+
When using a value expression in most place expression contexts, a temporary unnamed memory location is created and initialized to that value.
211
+
The expression evaluates to that location instead, except if [promoted] to a `static`.
212
+
The [drop scope] of the temporary is usually the end of the enclosing statement.
235
213
236
214
### Implicit Borrows
237
215
238
-
Certain expressions will treat an expression as a place expression by implicitly
239
-
borrowing it. For example, it is possible to compare two unsized [slices][slice] for
240
-
equality directly, because the `==` operator implicitly borrows its operands:
216
+
Certain expressions will treat an expression as a place expression by implicitly borrowing it.
217
+
For example, it is possible to compare two unsized [slices][slice] for equality directly, because the `==` operator implicitly borrows its operands:
241
218
242
219
```rust
243
220
# letc= [1, 2, 3];
@@ -264,31 +241,21 @@ Implicit borrows may be taken in the following expressions:
264
241
265
242
## Overloading Traits
266
243
267
-
Many of the following operators and expressions can also be overloaded for
268
-
other types using traits in `std::ops` or `std::cmp`. These traits also
269
-
exist in `core::ops` and `core::cmp` with the same names.
244
+
Many of the following operators and expressions can also be overloaded for other types using traits in `std::ops` or `std::cmp`.
245
+
These traits also exist in `core::ops` and `core::cmp` with the same names.
270
246
271
247
## Expression Attributes
272
248
273
-
[Outer attributes][_OuterAttribute_] before an expression are allowed only in
274
-
a few specific cases:
249
+
[Outer attributes][_OuterAttribute_] before an expression are allowed only in a few specific cases:
275
250
276
251
* Before an expression used as a [statement].
277
-
* Elements of [array expressions], [tuple expressions], [call expressions],
278
-
and tuple-style [struct] expressions.
279
-
<!--
280
-
These were likely stabilized inadvertently.
281
-
See https://github.com/rust-lang/rust/issues/32796 and
Rust is _primarily_ an expression language. This means that most forms of
4
-
value-producing or effect-causing evaluation are directed by the uniform syntax
5
-
category of _expressions_. Each kind of expression can typically _nest_ within
6
-
each other kind of expression, and rules for evaluation of expressions involve
7
-
specifying both the value produced by the expression and the order in which its
8
-
sub-expressions are themselves evaluated.
3
+
Rust is _primarily_ an expression language.
4
+
This means that most forms of value-producing or effect-causing evaluation are directed by the uniform syntax category of _expressions_.
5
+
Each kind of expression can typically _nest_ within each other kind of expression, and rules for evaluation of expressions involve specifying both the value produced by the expression and the order in which its sub-expressions are themselves evaluated.
9
6
10
-
In contrast, statements serve _mostly_ to contain and explicitly
11
-
sequence expression evaluation.
7
+
In contrast, statements serve _mostly_ to contain and explicitly sequence expression evaluation.
0 commit comments