@@ -79,17 +79,17 @@ Here are some examples:
79
79
80
80
### Moved and copied types
81
81
82
- When a [ local variable] ( # variables) is used as an
83
- [ rvalue] ( #lvalues-rvalues-and-temporaries ) , the variable will be copied
84
- if its type implements ` Copy ` . All others are moved.
82
+ When a [ local variable] ( variables.html ) is used as an
83
+ [ rvalue] ( expressions.html #lvalues-rvalues-and-temporaries) , the variable will
84
+ be copied if its type implements ` Copy ` . All others are moved.
85
85
86
86
## Literal expressions
87
87
88
- A _ literal expression_ consists of one of the [ literal] ( #literals ) forms
88
+ A _ literal expression_ consists of one of the [ literal] ( tokens.md #literals) forms
89
89
described earlier. It directly describes a number, character, string, boolean
90
90
value, or the unit value.
91
91
92
- ``` {.literals}
92
+ ``` text
93
93
(); // unit type
94
94
"hello"; // string type
95
95
'5'; // character type
@@ -98,13 +98,15 @@ value, or the unit value.
98
98
99
99
## Path expressions
100
100
101
- A [ path] ( #paths ) used as an expression context denotes either a local variable
102
- or an item. Path expressions are [ lvalues] ( #lvalues-rvalues-and-temporaries ) .
101
+ A [ path] ( paths.html ) used as an expression context denotes either a local
102
+ variable or an item. Path expressions are
103
+ [ lvalues] ( expressions.html#lvalues-rvalues-and-temporaries ) .
103
104
104
105
## Tuple expressions
105
106
106
107
Tuples are written by enclosing zero or more comma-separated expressions in
107
- parentheses. They are used to create [ tuple-typed] ( #tuple-types ) values.
108
+ parentheses. They are used to create [ tuple-typed] ( types.html#tuple-types )
109
+ values.
108
110
109
111
``` {.tuple}
110
112
(0.0, 4.5);
@@ -122,21 +124,20 @@ comma:
122
124
## Struct expressions
123
125
124
126
There are several forms of struct expressions. A _ struct expression_
125
- consists of the [ path] ( #paths ) of a [ struct item] ( #structs ) , followed by
126
- a brace-enclosed list of zero or more comma-separated name-value pairs,
127
- providing the field values of a new instance of the struct. A field name
128
- can be any identifier, and is separated from its value expression by a colon.
129
- The location denoted by a struct field is mutable if and only if the
130
- enclosing struct is mutable.
127
+ consists of the [ path] ( #paths ) of a [ struct item] ( items.html #structs) , followed
128
+ by a brace-enclosed list of zero or more comma-separated name-value pairs,
129
+ providing the field values of a new instance of the struct. A field name can be
130
+ any identifier, and is separated from its value expression by a colon. The
131
+ location denoted by a struct field is mutable if and only if the enclosing
132
+ struct is mutable.
131
133
132
134
A _ tuple struct expression_ consists of the [ path] ( #paths ) of a [ struct
133
- item] ( #structs ) , followed by a parenthesized list of one or more
134
- comma-separated expressions (in other words, the path of a struct item
135
- followed by a tuple expression). The struct item must be a tuple struct
136
- item.
135
+ item] ( items.html#structs ) , followed by a parenthesized list of one or more
136
+ comma-separated expressions (in other words, the path of a struct item followed
137
+ by a tuple expression). The struct item must be a tuple struct item.
137
138
138
139
A _ unit-like struct expression_ consists only of the [ path] ( #paths ) of a
139
- [ struct item] ( #structs ) .
140
+ [ struct item] ( items.html #structs) .
140
141
141
142
The following are examples of struct expressions:
142
143
@@ -216,24 +217,24 @@ A _method call_ consists of an expression followed by a single dot, an
216
217
identifier, and a parenthesized expression-list. Method calls are resolved to
217
218
methods on specific traits, either statically dispatching to a method if the
218
219
exact ` self ` -type of the left-hand-side is known, or dynamically dispatching if
219
- the left-hand-side expression is an indirect [ trait object] ( # trait-objects) .
220
+ the left-hand-side expression is an indirect [ trait object] ( trait-objects.html ) .
220
221
221
222
## Field expressions
222
223
223
224
A _ field expression_ consists of an expression followed by a single dot and an
224
225
identifier, when not immediately followed by a parenthesized expression-list
225
226
(the latter is a [ method call expression] ( #method-call-expressions ) ). A field
226
- expression denotes a field of a [ struct] ( #struct-types ) .
227
+ expression denotes a field of a [ struct] ( types.html #struct-types) .
227
228
228
229
``` {.ignore .field}
229
230
mystruct.myfield;
230
231
foo().x;
231
232
(Struct {a: 10, b: 20}).a;
232
233
```
233
234
234
- A field access is an [ lvalue] ( #lvalues-rvalues-and-temporaries ) referring to
235
- the value of that field. When the type providing the field inherits mutability,
236
- it can be [ assigned] ( #assignment-expressions ) to.
235
+ A field access is an [ lvalue] ( expressions.html #lvalues-rvalues-and-temporaries)
236
+ referring to the value of that field. When the type providing the field
237
+ inherits mutability, it can be [ assigned] ( #assignment-expressions ) to.
237
238
238
239
Also, if the type of the expression to the left of the dot is a
239
240
pointer, it is automatically dereferenced as many times as necessary
@@ -242,12 +243,13 @@ fewer autoderefs to more.
242
243
243
244
## Array expressions
244
245
245
- An [ array] ( #array-and-slice-types ) _ expression_ is written by enclosing zero
246
- or more comma-separated expressions of uniform type in square brackets.
246
+ An [ array] ( types.html#array-and-slice-types ) _ expression_ is written by
247
+ enclosing zero or more comma-separated expressions of uniform type in square
248
+ brackets.
247
249
248
250
In the ` [expr ';' expr] ` form, the expression after the ` ';' ` must be a
249
251
constant expression that can be evaluated at compile time, such as a
250
- [ literal] ( #literals ) or a [ static item] ( #static-items ) .
252
+ [ literal] ( tokens.html #literals) or a [ static item] ( items.html #static-items) .
251
253
252
254
```
253
255
[1, 2, 3, 4];
@@ -258,14 +260,15 @@ constant expression that can be evaluated at compile time, such as a
258
260
259
261
## Index expressions
260
262
261
- [ Array] ( #array-and-slice-types ) -typed expressions can be indexed by
263
+ [ Array] ( types.html #array-and-slice-types) -typed expressions can be indexed by
262
264
writing a square-bracket-enclosed expression (the index) after them. When the
263
- array is mutable, the resulting [ lvalue ] ( #lvalues-rvalues-and-temporaries ) can
264
- be assigned to.
265
+ array is mutable, the resulting
266
+ [ lvalue ] ( expressions.html#lvalues-rvalues-and-temporaries ) can be assigned to.
265
267
266
268
Indices are zero-based, and may be of any integral type. Vector access is
267
- bounds-checked at compile-time for constant arrays being accessed with a constant index value.
268
- Otherwise a check will be performed at run-time that will put the thread in a _ panicked state_ if it fails.
269
+ bounds-checked at compile-time for constant arrays being accessed with a
270
+ constant index value. Otherwise a check will be performed at run-time that
271
+ will put the thread in a _ panicked state_ if it fails.
269
272
270
273
``` {should-fail}
271
274
([1, 2, 3, 4])[0];
@@ -333,14 +336,15 @@ all written as prefix operators, before the expression they apply to.
333
336
is an error to apply negation to unsigned types; for example, the compiler
334
337
rejects ` -1u32 ` .
335
338
* ` * `
336
- : Dereference. When applied to a [ pointer] ( #pointer-types ) it denotes the
337
- pointed-to location. For pointers to mutable locations, the resulting
338
- [ lvalue] ( #lvalues-rvalues-and-temporaries ) can be assigned to.
339
- On non-pointer types, it calls the ` deref ` method of the ` std::ops::Deref `
340
- trait, or the ` deref_mut ` method of the ` std::ops::DerefMut ` trait (if
341
- implemented by the type and required for an outer expression that will or
342
- could mutate the dereference), and produces the result of dereferencing the
343
- ` & ` or ` &mut ` borrowed pointer returned from the overload method.
339
+ : Dereference. When applied to a [ pointer] ( types.html#pointer-types ) it
340
+ denotes the pointed-to location. For pointers to mutable locations, the
341
+ resulting [ lvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) can be
342
+ assigned to. On non-pointer types, it calls the ` deref ` method of the
343
+ ` std::ops::Deref ` trait, or the ` deref_mut ` method of the
344
+ ` std::ops::DerefMut ` trait (if implemented by the type and required for an
345
+ outer expression that will or could mutate the dereference), and produces
346
+ the result of dereferencing the ` & ` or ` &mut ` borrowed pointer returned
347
+ from the overload method.
344
348
* ` ! `
345
349
: Logical negation. On the boolean type, this flips between ` true ` and
346
350
` false ` . On integer types, this inverts the individual bits in the
@@ -480,8 +484,9 @@ surprising side-effects on the dynamic execution semantics.
480
484
### Assignment expressions
481
485
482
486
An _ assignment expression_ consists of an
483
- [ lvalue] ( #lvalues-rvalues-and-temporaries ) expression followed by an equals
484
- sign (` = ` ) and an [ rvalue] ( #lvalues-rvalues-and-temporaries ) expression.
487
+ [ lvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) expression followed
488
+ by an equals sign (` = ` ) and an
489
+ [ rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) expression.
485
490
486
491
Evaluating an assignment expression [ either copies or
487
492
moves] ( #moved-and-copied-types ) its right-hand operand to its left-hand
@@ -571,15 +576,16 @@ Lambda expressions are most useful when passing functions as arguments to other
571
576
functions, as an abbreviation for defining and capturing a separate function.
572
577
573
578
Significantly, lambda expressions _ capture their environment_ , which regular
574
- [ function definitions] ( #functions ) do not. The exact type of capture depends
575
- on the [ function type] ( #function-types ) inferred for the lambda expression. In
576
- the simplest and least-expensive form (analogous to a ``` || { } ``` expression),
577
- the lambda expression captures its environment by reference, effectively
578
- borrowing pointers to all outer variables mentioned inside the function.
579
- Alternately, the compiler may infer that a lambda expression should copy or
580
- move values (depending on their type) from the environment into the lambda
581
- expression's captured environment. A lambda can be forced to capture its
582
- environment by moving values by prefixing it with the ` move ` keyword.
579
+ [ function definitions] ( items.html#functions ) do not. The exact type of capture
580
+ depends on the [ function type] ( types.html#function-types ) inferred for the
581
+ lambda expression. In the simplest and least-expensive form (analogous to a
582
+ ``` || { } ``` expression), the lambda expression captures its environment by
583
+ reference, effectively borrowing pointers to all outer variables mentioned
584
+ inside the function. Alternately, the compiler may infer that a lambda
585
+ expression should copy or move values (depending on their type) from the
586
+ environment into the lambda expression's captured environment. A lambda can be
587
+ forced to capture its environment by moving values by prefixing it with the
588
+ ` move ` keyword.
583
589
584
590
In this example, we define a function ` ten_times ` that takes a higher-order
585
591
function argument, and we then call it with a lambda expression as an argument,
@@ -715,12 +721,13 @@ stands for a *single* data field, whereas a wildcard `..` stands for *all* the
715
721
fields of a particular variant.
716
722
717
723
A ` match ` behaves differently depending on whether or not the head expression
718
- is an [ lvalue or an rvalue] ( #lvalues-rvalues-and-temporaries ) . If the head
719
- expression is an rvalue, it is first evaluated into a temporary location, and
720
- the resulting value is sequentially compared to the patterns in the arms until
721
- a match is found. The first arm with a matching pattern is chosen as the branch
722
- target of the ` match ` , any variables bound by the pattern are assigned to local
723
- variables in the arm's block, and control enters the block.
724
+ is an [ lvalue or an rvalue] ( expressions.html#lvalues-rvalues-and-temporaries ) .
725
+ If the head expression is an rvalue, it is first evaluated into a temporary
726
+ location, and the resulting value is sequentially compared to the patterns in
727
+ the arms until a match is found. The first arm with a matching pattern is
728
+ chosen as the branch target of the ` match ` , any variables bound by the pattern
729
+ are assigned to local variables in the arm's block, and control enters the
730
+ block.
724
731
725
732
When the head expression is an lvalue, the match does not allocate a temporary
726
733
location (however, a by-value binding may copy or move from the lvalue). When
0 commit comments