Skip to content

Commit 4305b67

Browse files
committed
statements and expressions
1 parent 0c5e17a commit 4305b67

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

src/doc/reference/src/expressions.md

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ Here are some examples:
7979

8080
### Moved and copied types
8181

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.
8585

8686
## Literal expressions
8787

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
8989
described earlier. It directly describes a number, character, string, boolean
9090
value, or the unit value.
9191

92-
```{.literals}
92+
```text
9393
(); // unit type
9494
"hello"; // string type
9595
'5'; // character type
@@ -98,13 +98,15 @@ value, or the unit value.
9898

9999
## Path expressions
100100

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).
103104

104105
## Tuple expressions
105106

106107
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.
108110

109111
```{.tuple}
110112
(0.0, 4.5);
@@ -122,21 +124,20 @@ comma:
122124
## Struct expressions
123125

124126
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.
131133

132134
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.
137138

138139
A _unit-like struct expression_ consists only of the [path](#paths) of a
139-
[struct item](#structs).
140+
[struct item](items.html#structs).
140141

141142
The following are examples of struct expressions:
142143

@@ -216,24 +217,24 @@ A _method call_ consists of an expression followed by a single dot, an
216217
identifier, and a parenthesized expression-list. Method calls are resolved to
217218
methods on specific traits, either statically dispatching to a method if the
218219
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).
220221

221222
## Field expressions
222223

223224
A _field expression_ consists of an expression followed by a single dot and an
224225
identifier, when not immediately followed by a parenthesized expression-list
225226
(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).
227228

228229
```{.ignore .field}
229230
mystruct.myfield;
230231
foo().x;
231232
(Struct {a: 10, b: 20}).a;
232233
```
233234

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.
237238

238239
Also, if the type of the expression to the left of the dot is a
239240
pointer, it is automatically dereferenced as many times as necessary
@@ -242,12 +243,13 @@ fewer autoderefs to more.
242243

243244
## Array expressions
244245

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.
247249

248250
In the `[expr ';' expr]` form, the expression after the `';'` must be a
249251
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).
251253

252254
```
253255
[1, 2, 3, 4];
@@ -258,14 +260,15 @@ constant expression that can be evaluated at compile time, such as a
258260

259261
## Index expressions
260262

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
262264
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.
265267

266268
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.
269272

270273
```{should-fail}
271274
([1, 2, 3, 4])[0];
@@ -333,14 +336,15 @@ all written as prefix operators, before the expression they apply to.
333336
is an error to apply negation to unsigned types; for example, the compiler
334337
rejects `-1u32`.
335338
* `*`
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.
344348
* `!`
345349
: Logical negation. On the boolean type, this flips between `true` and
346350
`false`. On integer types, this inverts the individual bits in the
@@ -480,8 +484,9 @@ surprising side-effects on the dynamic execution semantics.
480484
### Assignment expressions
481485

482486
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.
485490

486491
Evaluating an assignment expression [either copies or
487492
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
571576
functions, as an abbreviation for defining and capturing a separate function.
572577

573578
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.
583589

584590
In this example, we define a function `ten_times` that takes a higher-order
585591
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
715721
fields of a particular variant.
716722

717723
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.
724731

725732
When the head expression is an lvalue, the match does not allocate a temporary
726733
location (however, a by-value binding may copy or move from the lvalue). When

src/doc/reference/src/statements.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Statements
22

33
A _statement_ is a component of a block, which is in turn a component of an
4-
outer [expression](#expressions) or [function](#functions).
4+
outer [expression](expressions.html) or [function](items.html#functions).
55

66
Rust has two kinds of statement: [declaration
77
statements](#declaration-statements) and [expression
@@ -16,7 +16,7 @@ items.
1616
### Item declarations
1717

1818
An _item declaration statement_ has a syntactic form identical to an
19-
[item](#items) declaration within a module. Declaring an item — a
19+
[item](items.html) declaration within a module. Declaring an item — a
2020
function, enumeration, struct, type, static, trait, implementation or module
2121
— locally within a statement block is simply a way of restricting its
2222
scope to a narrow region containing all of its uses; it is otherwise identical
@@ -36,7 +36,7 @@ declaration until the end of the enclosing block scope.
3636

3737
## Expression statements
3838

39-
An _expression statement_ is one that evaluates an [expression](#expressions)
39+
An _expression statement_ is one that evaluates an [expression](expressions.html)
4040
and ignores its result. The type of an expression statement `e;` is always
4141
`()`, regardless of the type of `e`. As a rule, an expression statement's
4242
purpose is to trigger the effects of evaluating its expression.

0 commit comments

Comments
 (0)