Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document label_break_value in the reference #1263

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/expressions/block-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ unsafe {
let a = unsafe { an_unsafe_fn() };
```

## Labelled block expressions

Labelled block expressions are documented in the [Loops and other breakable expressions] section.

## Attributes on block expressions

[Inner attributes] are allowed directly after the opening brace of a block expression in the following situations:
Expand Down Expand Up @@ -189,3 +193,4 @@ fn is_unix_platform() -> bool {
[tuple expressions]: tuple-expr.md
[unsafe operations]: ../unsafety.md
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
[Loops and other breakable expressions]: loop-expr.md#labelled-block-expressions
34 changes: 30 additions & 4 deletions src/expressions/loop-expr.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Loops
# Loops and other breakable expressions

> **<sup>Syntax</sup>**\
> _LoopExpression_ :\
Expand All @@ -7,23 +7,27 @@
> &nbsp;&nbsp; &nbsp;&nbsp; | [_PredicateLoopExpression_]\
> &nbsp;&nbsp; &nbsp;&nbsp; | [_PredicatePatternLoopExpression_]\
> &nbsp;&nbsp; &nbsp;&nbsp; | [_IteratorLoopExpression_]\
> &nbsp;&nbsp; &nbsp;&nbsp; | [_LabelBlockExpression_]\
> &nbsp;&nbsp; )

[_LoopLabel_]: #loop-labels
[_InfiniteLoopExpression_]: #infinite-loops
[_PredicateLoopExpression_]: #predicate-loops
[_PredicatePatternLoopExpression_]: #predicate-pattern-loops
[_IteratorLoopExpression_]: #iterator-loops
[_LabelBlockExpression_]: #labelled-block-expressions

Rust supports four loop expressions:
Rust supports five loop expressions:

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

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

## Infinite loops

Expand Down Expand Up @@ -193,6 +197,18 @@ A loop expression may optionally have a _label_. The label is written as a lifet
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.
See [break expressions](#break-expressions) and [continue expressions](#continue-expressions).

Labels follow the hygiene and shadowing rules of local variables. For example, this code will print "outer loop":

```rust
'a: loop {
'a: loop {
break 'a;
}
print!("outer loop");
break 'a;
}
```

## `break` expressions

> **<sup>Syntax</sup>**\
Expand Down Expand Up @@ -226,6 +242,16 @@ Example:

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`.

## Labelled block expressions

> **<sup>Syntax</sup>**\
> _LabelBlockExpression_ :\
> &nbsp;&nbsp; [_BlockExpression_]

Labelled block expressions are exactly like block expressions, except that they allow using `break` expressions within the block.
Unlike other loops, `break` expressions within a label expression *must* have a label (i.e. the label is not optional).
Unlike other loops, labelled block expressions *must* begin with a label.

## `continue` expressions

> **<sup>Syntax</sup>**\
Expand Down