Skip to content

Commit 79fff80

Browse files
committed
Document let else statements
1 parent a7cdac3 commit 79fff80

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/statements.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,24 @@ 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_] | `=` [_Expression_] `else` [_BlockExpression_]) `;`
5858
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
59+
A *`let` statement* introduces a new set of [variables], given by a [pattern].
60+
The pattern is followed optionally by a type annotation and then either ends,
61+
or is followed by an initializer expression plus an optional `else` block.
62+
When no type annotation is given, the compiler will infer the type, or signal
6363
an error if insufficient type information is available for definite
6464
inference. Any variables introduced by a variable declaration are visible
6565
from the point of declaration until the end of the enclosing block scope,
6666
except when they are shadowed by another variable declaration.
6767

68+
If an `else` block is not present, the pattern has to be irrefutable.
69+
If an `else` block is present, the pattern may be refutable, and if it is, it
70+
will be checked on run time. On a mismatch, the `else` block is executed.
71+
The `else` block has to always diverge (evaluate to type `!`).
72+
If an `else` block is present, restrictions apply on the expression:
73+
It might not be a [_LazyBooleanExpression_], or end in a `}` token.
74+
6875
## Expression statements
6976

7077
> **<sup>Syntax</sup>**\
@@ -128,9 +135,11 @@ statement are [`cfg`], and [the lint check attributes].
128135
[`cfg`]: conditional-compilation.md
129136
[the lint check attributes]: attributes/diagnostics.md#lint-check-attributes
130137
[pattern]: patterns.md
138+
[_BlockExpression_]: expressions/block-expr.md
131139
[_ExpressionStatement_]: #expression-statements
132140
[_Expression_]: expressions.md
133141
[_Item_]: items.md
142+
[_LazyBooleanExpression_]: expressions/operator-expr.md#lazy-boolean-operators
134143
[_LetStatement_]: #let-statements
135144
[_MacroInvocationSemi_]: macros.md#macro-invocation
136145
[_OuterAttribute_]: attributes.md

0 commit comments

Comments
 (0)