Skip to content

Commit 13a6230

Browse files
authored
Merge pull request #1611 from chorman0773/spec-add-identifiers-statements
Add identifier syntax to statements.md
2 parents 7ce3563 + d107c04 commit 13a6230

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/statements-and-expressions.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Statements and expressions
22

3+
r[stmt-expr]
4+
35
Rust is _primarily_ an expression language.
46
This means that most forms of value-producing or effect-causing evaluation are directed by the uniform syntax category of _expressions_.
57
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.

src/statements.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Statements
22

3+
r[statement]
4+
5+
r[statement.syntax]
36
> **<sup>Syntax</sup>**\
47
> _Statement_ :\
58
> &nbsp;&nbsp; &nbsp;&nbsp; `;`\
@@ -8,26 +11,37 @@
811
> &nbsp;&nbsp; | [_ExpressionStatement_]\
912
> &nbsp;&nbsp; | [_MacroInvocationSemi_]
1013
11-
14+
r[statement.intro]
1215
A *statement* is a component of a [block], which is in turn a component of an outer [expression] or [function].
1316

17+
r[statement.kind]
1418
Rust has two kinds of statement: [declaration statements](#declaration-statements) and [expression statements](#expression-statements).
1519

1620
## Declaration statements
1721

22+
r[statement.decl]
23+
1824
A *declaration statement* is one that introduces one or more *names* into the enclosing statement block.
1925
The declared names may denote new variables or new [items][item].
2026

2127
The two kinds of declaration statements are item declarations and `let` statements.
2228

2329
### Item declarations
2430

31+
r[statement.item]
32+
33+
r[statement.item.intro]
2534
An *item declaration statement* has a syntactic form identical to an [item declaration][item] within a [module].
35+
36+
r[statement.item.scope]
2637
Declaring an item within a statement block restricts its [scope] to the block containing the statement.
2738
The item is not given a [canonical path] nor are any sub-items it may declare.
39+
40+
r[statement.item.associated-scope]
2841
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.
2942
It is otherwise identical in meaning to declaring the item inside a module.
3043

44+
r[statement.item.outer-generics]
3145
There is no implicit capture of the containing function's generic parameters, parameters, and local variables.
3246
For example, `inner` may not access `outer_var`.
3347

@@ -43,6 +57,9 @@ fn outer() {
4357

4458
### `let` statements
4559

60+
r[statement.let]
61+
62+
r[statement.let.syntax]
4663
> **<sup>Syntax</sup>**\
4764
> _LetStatement_ :\
4865
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> `let` [_PatternNoTopAlt_]
@@ -52,13 +69,21 @@ fn outer() {
5269
> <span id="let-else-restriction">† When an `else` block is specified, the
5370
> _Expression_ must not be a [_LazyBooleanExpression_], or end with a `}`.</span>
5471
72+
r[statement.let.intro]
5573
A *`let` statement* introduces a new set of [variables], given by a [pattern].
5674
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]
5777
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]
5880
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.
5981

82+
r[statement.let.constraint]
6083
If an `else` block is not present, the pattern must be irrefutable.
6184
If an `else` block is present, the pattern may be refutable.
85+
86+
r[statement.let.behavior]
6287
If the pattern does not match (this requires it to be refutable), the `else` block is executed.
6388
The `else` block must always diverge (evaluate to the [never type]).
6489

@@ -75,17 +100,24 @@ let [u, v] = [v[0], v[1]] else { // This pattern is irrefutable, so the compiler
75100

76101
## Expression statements
77102

103+
r[statement.expr]
104+
105+
r[statement.expr.syntax]
78106
> **<sup>Syntax</sup>**\
79107
> _ExpressionStatement_ :\
80108
> &nbsp;&nbsp; &nbsp;&nbsp; [_ExpressionWithoutBlock_][expression] `;`\
81109
> &nbsp;&nbsp; | [_ExpressionWithBlock_][expression] `;`<sup>?</sup>
82110
111+
r[statement.expr.intro]
83112
An *expression statement* is one that evaluates an [expression] and ignores its result.
84113
As a rule, an expression statement's purpose is to trigger the effects of evaluating its expression.
85114

115+
r[statement.expr.restriction-semicolon]
86116
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.
87117
This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression;
88118
in this case, it is parsed as a statement.
119+
120+
r[statement.expr.constraint-block]
89121
The type of [_ExpressionWithBlock_][expression] expressions when used as statements must be the unit type.
90122

91123
```rust
@@ -118,6 +150,8 @@ if true {
118150

119151
## Attributes on Statements
120152

153+
r[statement.attribute]
154+
121155
Statements accept [outer attributes].
122156
The attributes that have meaning on a statement are [`cfg`], and [the lint check attributes].
123157

0 commit comments

Comments
 (0)