Skip to content

Commit 9134fe5

Browse files
chorman0773ehuss
authored andcommitted
Add paragraph identifiers to expressions.md
1 parent 75df12e commit 9134fe5

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/expressions.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Expressions
22

3+
r[expr]
4+
5+
r[expr.syntax]
36
> **<sup>Syntax</sup>**\
47
> _Expression_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; _ExpressionWithoutBlock_\
@@ -43,22 +46,32 @@
4346
> &nbsp;&nbsp; &nbsp;&nbsp; | [_MatchExpression_]\
4447
> &nbsp;&nbsp; )
4548
49+
r[expr.intro]
4650
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]
4753
An expression *evaluates to* a value, and has effects during *evaluation*.
54+
55+
r[expr.operands]
4856
Many expressions contain sub-expressions, called the *operands* of the expression.
57+
58+
r[expr.behaviour]
4959
The meaning of each kind of expression dictates several things:
5060

5161
* Whether or not to evaluate the operands when evaluating the expression
5262
* The order in which to evaluate the operands
5363
* How to combine the operands' values to obtain the value of the expression
5464

65+
r[expr.structure]
5566
In this way, the structure of expressions dictates the structure of execution.
5667
Blocks are just another kind of expression, so blocks, statements, expressions, and blocks again can recursively nest inside each other to an arbitrary depth.
5768

5869
> **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.
5970
6071
## Expression precedence
6172

73+
r[expr.precedence]
74+
6275
The precedence of Rust operators and expressions is ordered as follows, going from strong to weak.
6376
Binary Operators at the same precedence level are grouped in the order given by their associativity.
6477

@@ -86,6 +99,9 @@ Binary Operators at the same precedence level are grouped in the order given by
8699

87100
## Evaluation order of operands
88101

102+
r[expr.operand-order]
103+
104+
r[expr.operand-order.default]
89105
The following list of expressions all evaluate their operands the same way, as described after the list.
90106
Other expressions either don't take operands or evaluate them conditionally as described on their respective pages.
91107

@@ -109,6 +125,7 @@ Other expressions either don't take operands or evaluate them conditionally as d
109125
* Range expression
110126
* Return expression
111127

128+
r[expr.operand-order.operands-before-primary]
112129
The operands of these expressions are evaluated prior to applying the effects of the expression.
113130
Expressions taking multiple operands are evaluated left to right as written in the source code.
114131

@@ -132,17 +149,27 @@ assert_eq!(
132149
133150
## Place Expressions and Value Expressions
134151

152+
r[expr.place-value]
153+
154+
r[expr.place-value.intro]
135155
Expressions are divided into two main categories: place expressions and value expressions;
136156
there is also a third, minor category of expressions called assignee expressions.
137157
Within each expression, operands may likewise occur in either place context or value context.
138158
The evaluation of an expression depends both on its own category and the context it occurs within.
139159

160+
r[expr.place-value.place-memory-location]
140161
A *place expression* is an expression that represents a memory location.
162+
163+
r[expr.place-value.place-expr-kinds]
141164
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]
142167
All other expressions are value expressions.
143168

169+
r[expr.place-value.value-result]
144170
A *value expression* is an expression that represents an actual value.
145171

172+
r[expr.place-value.place-context]
146173
The following contexts are *place expression* contexts:
147174

148175
* The left operand of a [compound assignment] expression.
@@ -157,6 +184,7 @@ The following contexts are *place expression* contexts:
157184

158185
> Note: Historically, place expressions were called *lvalues* and value expressions were called *rvalues*.
159186
187+
r[expr.place-value.assignee]
160188
An *assignee expression* is an expression that appears in the left operand of an [assignment][assign] expression.
161189
Explicitly, the assignee expressions are:
162190

@@ -169,29 +197,46 @@ Explicitly, the assignee expressions are:
169197
fields).
170198
- [Unit structs][_StructExpression_].
171199

200+
r[expr.place-value.parentehesis]
172201
Arbitrary parenthesisation is permitted inside assignee expressions.
173202

174203
### Moved and copied types
175204

205+
r[expr.move]
206+
207+
r[expr.move.intro]
176208
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]
177211
If the type of that value implements [`Copy`], then the value will be copied.
212+
213+
r[expr.move.requires-sized]
178214
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]
179217
Only the following place expressions may be moved out of:
180218

181219
* [Variables] which are not currently borrowed.
182220
* [Temporary values](#temporaries).
183221
* [Fields][field] of a place expression which can be moved out of and don't implement [`Drop`].
184222
* The result of [dereferencing][deref] an expression with type [`Box<T>`] and that can also be moved out of.
185223

224+
r[expr.move.deinitialization]
186225
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]
187228
In all other cases, trying to use a place expression in a value expression context is an error.
188229

189230
### Mutability
190231

232+
r[expr.mut]
233+
234+
r[expr.mut.intro]
191235
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_.
192236
We call these *mutable place expressions*.
193237
In contrast, other place expressions are called *immutable place expressions*.
194238

239+
r[expr.mut.valid-places]
195240
The following expressions can be mutable place expression contexts:
196241

197242
* Mutable [variables] which are not currently borrowed.
@@ -208,12 +253,17 @@ The following expressions can be mutable place expression contexts:
208253

209254
### Temporaries
210255

256+
r[expr.temporary]
257+
211258
When using a value expression in most place expression contexts, a temporary unnamed memory location is created and initialized to that value.
212259
The expression evaluates to that location instead, except if [promoted] to a `static`.
213260
The [drop scope] of the temporary is usually the end of the enclosing statement.
214261

215262
### Implicit Borrows
216263

264+
r[expr.implicit-borrow]
265+
266+
r[expr.implicit-borrow-intro]
217267
Certain expressions will treat an expression as a place expression by implicitly borrowing it.
218268
For example, it is possible to compare two unsized [slices][slice] for equality directly, because the `==` operator implicitly borrows its operands:
219269

@@ -230,6 +280,7 @@ let b: &[i32];
230280
::std::cmp::PartialEq::eq(&*a, &*b);
231281
```
232282

283+
r[expr.implicit-borrow.application]
233284
Implicit borrows may be taken in the following expressions:
234285

235286
* Left operand in [method-call] expressions.
@@ -242,23 +293,28 @@ Implicit borrows may be taken in the following expressions:
242293

243294
## Overloading Traits
244295

296+
r[expr.overload]
297+
245298
Many of the following operators and expressions can also be overloaded for other types using traits in `std::ops` or `std::cmp`.
246299
These traits also exist in `core::ops` and `core::cmp` with the same names.
247300

248301
## Expression Attributes
249302

303+
r[expr.attr]
304+
305+
r[expr.attr.restriction]
250306
[Outer attributes][_OuterAttribute_] before an expression are allowed only in a few specific cases:
251307

252308
* Before an expression used as a [statement].
253309
* Elements of [array expressions], [tuple expressions], [call expressions], and tuple-style [struct] expressions.
254310
* The tail expression of [block expressions].
255311
<!-- Keep list in sync with block-expr.md -->
256312

313+
r[expr.attr.never-before]
257314
They are never allowed before:
258315
* [Range][_RangeExpression_] expressions.
259316
* Binary operator expressions ([_ArithmeticOrLogicalExpression_], [_ComparisonExpression_], [_LazyBooleanExpression_], [_TypeCastExpression_], [_AssignmentExpression_], [_CompoundAssignmentExpression_]).
260317

261-
262318
[block expressions]: expressions/block-expr.md
263319
[call expressions]: expressions/call-expr.md
264320
[field]: expressions/field-expr.md

0 commit comments

Comments
 (0)