Skip to content

Commit 9df3a29

Browse files
committed
Document let else statements
1 parent a7cdac3 commit 9df3a29

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/statements.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,27 @@ fn outer() {
5454
> **<sup>Syntax</sup>**\
5555
> _LetStatement_ :\
5656
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> `let` [_PatternNoTopAlt_]
57-
> ( `:` [_Type_] )<sup>?</sup> (`=` [_Expression_] )<sup>?</sup> `;`
57+
> ( `:` [_Type_] )<sup>?</sup> (`=` [_Expression_][](#let-else-restriction)
58+
> ( `else` [_BlockExpression_]) <sup>?</sup> ) <sup>?</sup> `;`
5859
59-
A *`let` statement* introduces a new set of [variables], given by an
60-
irrefutable [pattern]. The pattern is followed optionally by a type
61-
annotation and then optionally by an initializer expression. When no
62-
type annotation is given, the compiler will infer the type, or signal
60+
<span id="let-else-restriction">† When an `else` block is specified, the
61+
_Expression_ must not be a [_LazyBooleanExpression_], or end with a `}`.</span>
62+
63+
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
6367
an error if insufficient type information is available for definite
6468
inference. Any variables introduced by a variable declaration are visible
6569
from the point of declaration until the end of the enclosing block scope,
6670
except when they are shadowed by another variable declaration.
6771

72+
If an `else` block is not present, the pattern must be irrefutable.
73+
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.
76+
The `else` block must always diverge (evaluate to the [never type]).
77+
6878
## Expression statements
6979

7080
> **<sup>Syntax</sup>**\
@@ -121,16 +131,19 @@ statement are [`cfg`], and [the lint check attributes].
121131
[function]: items/functions.md
122132
[item]: items.md
123133
[module]: items/modules.md
134+
[never type]: types/never.md
124135
[canonical path]: paths.md#canonical-paths
125136
[implementations]: items/implementations.md
126137
[variables]: variables.md
127138
[outer attributes]: attributes.md
128139
[`cfg`]: conditional-compilation.md
129140
[the lint check attributes]: attributes/diagnostics.md#lint-check-attributes
130141
[pattern]: patterns.md
142+
[_BlockExpression_]: expressions/block-expr.md
131143
[_ExpressionStatement_]: #expression-statements
132144
[_Expression_]: expressions.md
133145
[_Item_]: items.md
146+
[_LazyBooleanExpression_]: expressions/operator-expr.md#lazy-boolean-operators
134147
[_LetStatement_]: #let-statements
135148
[_MacroInvocationSemi_]: macros.md#macro-invocation
136149
[_OuterAttribute_]: attributes.md

0 commit comments

Comments
 (0)