From 2ab614f3569f34eee2fd70862e8f35548282fb11 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 26 Oct 2012 14:05:21 -0700 Subject: [PATCH] Document labeled break and continue in the reference manual r=brson --- doc/rust.md | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/doc/rust.md b/doc/rust.md index 516504dffce..18beb742330 100644 --- a/doc/rust.md +++ b/doc/rust.md @@ -1951,32 +1951,48 @@ while i < 10 { ### Infinite loops -A `loop` expression denotes an infinite loop: +The keyword `loop` in Rust appears both in _loop expressions_ and in _continue expressions_. +A loop expression denotes an infinite loop; +see [Continue expressions](#continue-expressions) for continue 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 out of this loop or return control to its head. +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 +### Continue expressions ~~~~~~~~{.ebnf .gram} -loop_expr : "loop" ; +continue_expr : "loop" [ ident ]; ~~~~~~~~ -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. +A continue expression, written `loop`, 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.