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/statements-and-expressions.md
+2
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Statements and expressions
2
2
3
+
r[stmt-expr]
4
+
3
5
Rust is _primarily_ an expression language.
4
6
This means that most forms of value-producing or effect-causing evaluation are directed by the uniform syntax category of _expressions_.
5
7
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.
Copy file name to clipboardExpand all lines: src/statements.md
+35-1
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,8 @@
1
1
# Statements
2
2
3
+
r[statement]
4
+
5
+
r[statement.syntax]
3
6
> **<sup>Syntax</sup>**\
4
7
> _Statement_ :\
5
8
> `;`\
@@ -8,26 +11,37 @@
8
11
> | [_ExpressionStatement_]\
9
12
> | [_MacroInvocationSemi_]
10
13
11
-
14
+
r[statement.intro]
12
15
A *statement* is a component of a [block], which is in turn a component of an outer [expression] or [function].
13
16
17
+
r[statement.kind]
14
18
Rust has two kinds of statement: [declaration statements](#declaration-statements) and [expression statements](#expression-statements).
15
19
16
20
## Declaration statements
17
21
22
+
r[statement.decl]
23
+
18
24
A *declaration statement* is one that introduces one or more *names* into the enclosing statement block.
19
25
The declared names may denote new variables or new [items][item].
20
26
21
27
The two kinds of declaration statements are item declarations and `let` statements.
22
28
23
29
### Item declarations
24
30
31
+
r[statement.item]
32
+
33
+
r[statement.item.intro]
25
34
An *item declaration statement* has a syntactic form identical to an [item declaration][item] within a [module].
35
+
36
+
r[statement.item.scope]
26
37
Declaring an item within a statement block restricts its [scope] to the block containing the statement.
27
38
The item is not given a [canonical path] nor are any sub-items it may declare.
39
+
40
+
r[statement.item.associated-scope]
28
41
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.
29
42
It is otherwise identical in meaning to declaring the item inside a module.
30
43
44
+
r[statement.item.outer-generics]
31
45
There is no implicit capture of the containing function's generic parameters, parameters, and local variables.
> <spanid="let-else-restriction">† When an `else` block is specified, the
53
70
> _Expression_ must not be a [_LazyBooleanExpression_], or end with a `}`.</span>
54
71
72
+
r[statement.let.intro]
55
73
A *`let` statement* introduces a new set of [variables], given by a [pattern].
56
74
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.
75
+
76
+
r[statement.let.inference]
57
77
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.
78
+
79
+
r[statement.let.scope]
58
80
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.
59
81
82
+
r[statement.let.constraint]
60
83
If an `else` block is not present, the pattern must be irrefutable.
61
84
If an `else` block is present, the pattern may be refutable.
85
+
86
+
r[statement.let.behavior]
62
87
If the pattern does not match (this requires it to be refutable), the `else` block is executed.
63
88
The `else` block must always diverge (evaluate to the [never type]).
64
89
@@ -75,17 +100,24 @@ 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 result.
84
113
As a rule, an expression statement's purpose is to trigger the effects of evaluating its expression.
85
114
115
+
r[statement.expr.restriction-semicolon]
86
116
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
117
This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression;
88
118
in this case, it is parsed as a statement.
119
+
120
+
r[statement.expr.constraint-block]
89
121
The type of [_ExpressionWithBlock_][expression] expressions when used as statements must be the unit type.
90
122
91
123
```rust
@@ -118,6 +150,8 @@ if true {
118
150
119
151
## Attributes on Statements
120
152
153
+
r[statement.attribute]
154
+
121
155
Statements accept [outer attributes].
122
156
The attributes that have meaning on a statement are [`cfg`], and [the lint check attributes].
0 commit comments