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 labeled break and continue in the reference manual #3870

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 28 additions & 13 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1951,32 +1951,47 @@ while i < 10 {

### Infinite loops

A `loop` expression denotes an infinite loop:
The keyword `loop` in Rust appears in two different forms.
The first one denotes an infinite loop,
while the second one is described in [Loop expressions](#loop-expressions):

~~~~~~~~{.ebnf .gram}
loop_expr : "loop" '{' block '}';
loop_expr : "loop" [ ident ':' ] '{' block '}';
~~~~~~~~

A `loop` expression may optionally have a _label_.
If a label is present, then labeled `break` and `loop` expressions nested within this loop may exit to the top level.
See [Break expressions](#break-expressions).

### Break expressions

~~~~~~~~{.ebnf .gram}
break_expr : "break" ;
break_expr : "break" [ ident ];
~~~~~~~~

Executing a `break` expression immediately terminates the innermost loop
enclosing it. It is only permitted in the body of a loop.
A `break` expression has an optional `label`.
If the label is absent, then executing a `break` expression immediately terminates the innermost loop enclosing it.
It is only permitted in the body of a loop.
If the label is present, then `break foo` terminates the loop with label `foo`,
which need not be the innermost label enclosing the `break` expression,
but must enclose it.

### Loop expressions

~~~~~~~~{.ebnf .gram}
loop_expr : "loop" ;
~~~~~~~~

Evaluating a `loop` expression immediately terminates the current iteration of
the innermost loop enclosing it, returning control to the loop *head*. In the
case of a `while` loop, the head is the conditional expression controlling the
loop. In the case of a `for` loop, the head is the call-expression controlling
the loop.
loop_expr : "loop" [ ident ];
~~~~~~~~

A `loop` expression (the second form of the `loop` keyword) also has an optional `label`.
If the label is absent,
then executing a `loop` expression immediately terminates the current iteration of the innermost loop enclosing it,
returning control to the loop *head*.
In the case of a `while` loop,
the head is the conditional expression controlling the loop.
In the case of a `for` loop, the head is the call-expression controlling the loop.
If the label is present, then `loop foo` returns control to the head of the loop with label `foo`,
which need not be the innermost label enclosing the `break` expression,
but must enclose it.

A `loop` expression is only permitted in the body of a loop.

Expand Down