Skip to content

Commit 2973e62

Browse files
committed
Document label_break_value in the reference
1 parent 9fce337 commit 2973e62

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/expressions/block-expr.md

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ unsafe {
140140
let a = unsafe { an_unsafe_fn() };
141141
```
142142

143+
## Labelled block expressions
144+
145+
Labelled block expressions are documented in the [Loop expressions] section.
146+
143147
## Attributes on block expressions
144148

145149
[Inner attributes] are allowed directly after the opening brace of a block expression in the following situations:
@@ -189,3 +193,4 @@ fn is_unix_platform() -> bool {
189193
[tuple expressions]: tuple-expr.md
190194
[unsafe operations]: ../unsafety.md
191195
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
196+
[Loop expressions]: loop-expr.md#labelled-block-expressions

src/expressions/loop-expr.md

+29-3
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
>       | [_PredicateLoopExpression_]\
88
>       | [_PredicatePatternLoopExpression_]\
99
>       | [_IteratorLoopExpression_]\
10+
>       | [_LabelBlockExpression_]\
1011
>    )
1112
1213
[_LoopLabel_]: #loop-labels
1314
[_InfiniteLoopExpression_]: #infinite-loops
1415
[_PredicateLoopExpression_]: #predicate-loops
1516
[_PredicatePatternLoopExpression_]: #predicate-pattern-loops
1617
[_IteratorLoopExpression_]: #iterator-loops
18+
[_LabelBlockExpression_]: #labelled-block-expressions
1719

18-
Rust supports four loop expressions:
20+
Rust supports five loop expressions:
1921

2022
* A [`loop` expression](#infinite-loops) denotes an infinite loop.
2123
* A [`while` expression](#predicate-loops) loops until a predicate is false.
2224
* A [`while let` expression](#predicate-pattern-loops) tests a pattern.
2325
* A [`for` expression](#iterator-loops) extracts values from an iterator, looping until the iterator is empty.
26+
* A [labelled block expression](#labelled-block-expressions) runs a loop exactly once, but allows exiting the loop early with `break`.
2427

25-
All four types of loop support [`break` expressions](#break-expressions), [`continue` expressions](#continue-expressions), and [labels](#loop-labels).
26-
Only `loop` supports [evaluation to non-trivial values](#break-and-loop-values).
28+
All five types of loop support [`break` expressions](#break-expressions), and [labels](#loop-labels).
29+
All except labelled block expressions support [`continue` expressions](#continue-expressions).
30+
Only `loop` and labelled block expressions support [evaluation to non-trivial values](#break-and-loop-values).
2731

2832
## Infinite loops
2933

@@ -193,6 +197,18 @@ A loop expression may optionally have a _label_. The label is written as a lifet
193197
If a label is present, then labeled `break` and `continue` expressions nested within this loop may exit out of this loop or return control to its head.
194198
See [break expressions](#break-expressions) and [continue expressions](#continue-expressions).
195199

200+
Labels follow the hygiene and shadowing rules of local variables. For example, this code will print "outer loop":
201+
202+
```rust
203+
'a: loop {
204+
'a: loop {
205+
break 'a;
206+
}
207+
print!("outer loop");
208+
break 'a;
209+
}
210+
```
211+
196212
## `break` expressions
197213

198214
> **<sup>Syntax</sup>**\
@@ -226,6 +242,16 @@ Example:
226242

227243
A `break` expression is only permitted in the body of a loop, and has one of the forms `break`, `break 'label` or ([see below](#break-and-loop-values)) `break EXPR` or `break 'label EXPR`.
228244

245+
## Labelled block expressions
246+
247+
> **<sup>Syntax</sup>**\
248+
> _LabelBlockExpression_ :\
249+
> &nbsp;&nbsp; [_BlockExpression_]
250+
251+
Labelled block expressions are exactly like block expressions, except that they allow using `break` expressions within the block.
252+
Unlike other loops, `break` expressions within a label expression *must* have a label (i.e. the label is not optional).
253+
Unlike other loops, labelled block expressions *must* begin with a label.
254+
229255
## `continue` expressions
230256

231257
> **<sup>Syntax</sup>**\

0 commit comments

Comments
 (0)