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
+9
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,15 @@ assert_eq!(
129
129
130
130
> **Note**: Since this is applied recursively, these expressions are also evaluated from innermost to outermost, ignoring siblings until there are no inner subexpressions.
131
131
132
+
## Early Termination
133
+
134
+
Expressions may be *terminated* early, whether by a jump expression or unwinding.
135
+
When terminated, evaluation of the expression stops.
136
+
If the expression is the target of termination by a jump expression, then it evaluates to the value specified by the jump expression.
137
+
Otherwise, it evaluates to the never type.
138
+
139
+
**Note**: Destructors are still executed for the expression after being terminated.
140
+
132
141
## Place Expressions and Value Expressions
133
142
134
143
Expressions are divided into two main categories: place expressions and value expressions;
Copy file name to clipboardExpand all lines: src/statements.md
+23-39
Original file line number
Diff line number
Diff line change
@@ -9,35 +9,27 @@
9
9
> | [_MacroInvocationSemi_]
10
10
11
11
12
-
A *statement* is a component of a [block], which is in turn a component of an
13
-
outer [expression] or [function].
12
+
A *statement* is a component of a [block], which is in turn a component of an outer [expression] or [function].
14
13
15
-
Rust has two kinds of statement: [declaration
16
-
statements](#declaration-statements) and [expression
17
-
statements](#expression-statements).
14
+
Rust has two kinds of statement: [declaration statements](#declaration-statements) and [expression statements](#expression-statements).
18
15
19
16
## Declaration statements
20
17
21
-
A *declaration statement* is one that introduces one or more *names* into the
22
-
enclosing statement block. The declared names may denote new variables or new
23
-
[items][item].
18
+
A *declaration statement* is one that introduces one or more *names* into the enclosing statement block.
19
+
The declared names may denote new variables or new [items][item].
24
20
25
-
The two kinds of declaration statements are item declarations and `let`
26
-
statements.
21
+
The two kinds of declaration statements are item declarations and `let` statements.
27
22
28
23
### Item declarations
29
24
30
-
An *item declaration statement* has a syntactic form identical to an
31
-
[item declaration][item] within a [module]. Declaring an item within a statement
32
-
block restricts its scope to the block containing the statement. The item is not
33
-
given a [canonical path] nor are any sub-items it may declare. The exception to
34
-
this is that associated items defined by [implementations] are still accessible
35
-
in outer scopes as long as the item and, if applicable, trait are accessible.
25
+
An *item declaration statement* has a syntactic form identical to an [item declaration][item] within a [module].
26
+
Declaring an item within a statement block restricts its scope to the block containing the statement.
27
+
The item is not given a [canonical path] nor are any sub-items it may declare.
28
+
The exception to this is that associated items defined by [implementations] are still accessible in outer scopes as long as the item and, if applicable, trait are accessible.
36
29
It is otherwise identical in meaning to declaring the item inside a module.
37
30
38
-
There is no implicit capture of the containing function's generic parameters,
39
-
parameters, and local variables. For example, `inner` may not access
40
-
`outer_var`.
31
+
There is no implicit capture of the containing function's generic parameters, parameters, and local variables.
32
+
For example, `inner` may not access `outer_var`.
41
33
42
34
```rust
43
35
fnouter() {
@@ -61,18 +53,13 @@ fn outer() {
61
53
> _Expression_ must not be a [_LazyBooleanExpression_], or end with a `}`.</span>
62
54
63
55
A *`let` statement* introduces a new set of [variables], given by a [pattern].
64
-
The pattern is followed optionally by a type annotation and then either ends,
65
-
or is followed by an initializer expression plus an optional `else` block.
66
-
When no type annotation is given, the compiler will infer the type, or signal
67
-
an error if insufficient type information is available for definite
68
-
inference. Any variables introduced by a variable declaration are visible
69
-
from the point of declaration until the end of the enclosing block scope,
70
-
except when they are shadowed by another variable declaration.
56
+
The pattern is followed optionally by a type annotation and then either ends, or is followed by an initializer expression plus an optional `else` block.
57
+
When no type annotation is given, the compiler will infer the type, or signal an error if insufficient type information is available for definite inference.
58
+
Any variables introduced by a variable declaration are visible from the point of declaration until the end of the enclosing block scope, except when they are shadowed by another variable declaration.
71
59
72
60
If an `else` block is not present, the pattern must be irrefutable.
73
61
If an `else` block is present, the pattern may be refutable.
74
-
If the pattern does not match (this requires it to be refutable), the `else`
75
-
block is executed.
62
+
If the pattern does not match (this requires it to be refutable), the `else` block is executed.
76
63
The `else` block must always diverge (evaluate to the [never type]).
77
64
78
65
```rust
@@ -93,16 +80,13 @@ let [u, v] = [v[0], v[1]] else { // This pattern is irrefutable, so the compiler
An *expression statement* is one that evaluates an [expression] and ignores its
97
-
result. As a rule, an expression statement's purpose is to trigger the effects
98
-
of evaluating its expression.
83
+
An *expression statement* is one that evaluates an [expression] and ignores its result.
84
+
As a rule, an expression statement's purpose is to trigger the effects of evaluating its expression.
99
85
100
-
An expression that consists of only a [block expression][block] or control flow
101
-
expression, if used in a context where a statement is permitted, can omit the
102
-
trailing semicolon. This can cause an ambiguity between it being parsed as a
103
-
standalone statement and as a part of another expression; in this case, it is
104
-
parsed as a statement. The type of [_ExpressionWithBlock_][expression]
105
-
expressions when used as statements must be the unit type.
86
+
An expression that consists of only a [block expression][block] or control flow expression, if used in a context where a statement is permitted, can omit the trailing semicolon.
87
+
This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression;
88
+
in this case, it is parsed as a statement.
89
+
The type of [_ExpressionWithBlock_][expression] expressions when used as statements must be the unit type.
106
90
107
91
```rust
108
92
# letmutv=vec![1, 2, 3];
@@ -134,8 +118,8 @@ if true {
134
118
135
119
## Attributes on Statements
136
120
137
-
Statements accept [outer attributes]. The attributes that have meaning on a
138
-
statement are [`cfg`], and [the lint check attributes].
121
+
Statements accept [outer attributes].
122
+
The attributes that have meaning on a statement are [`cfg`], and [the lint check attributes].
0 commit comments