From e2b1736674fa9805c9f4aeb68d3fc56116f88639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Sat, 14 Oct 2017 13:19:54 -0300 Subject: [PATCH 1/3] Loops grammar --- src/expressions/loop-expr.md | 54 +++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/expressions/loop-expr.md b/src/expressions/loop-expr.md index 37ba2f21b..bc99b7d5c 100644 --- a/src/expressions/loop-expr.md +++ b/src/expressions/loop-expr.md @@ -1,5 +1,20 @@ # Loops +> **Syntax** +> _LoopExpression_ : +>    [_LoopLabel_]? ( +>          [_InfiniteLoopExpression_] +>       | [_PredicateLoopExpression_] +>       | [_IteratorLoopExpression_] +>       | [_WhileLetExpression_] +>    ) + +[_LoopLabel_]: #loop-labels +[_InfiniteLoopExpression_]: #infinite-loops +[_PredicateLoopExpression_]: #predicate-loops +[_IteratorLoopExpression_]: #iterator-loops +[_WhileLetExpression_]: #while-let-loops + Rust supports four loop expressions: * A [`loop` expression](#infinite-loops) denotes an infinite loop. @@ -14,6 +29,10 @@ Only `loop` supports [evaluation to non-trivial values](#break-and-loop-values). ## Infinite loops +> **Syntax** +> _InfiniteLoopExpression_ : +>    `loop` [_BlockExpression_] + A `loop` expression repeats execution of its body continuously: `loop { println!("I live."); }`. @@ -26,6 +45,10 @@ expression(s). ## Predicate loops +> **Syntax** +> _PredicateLoopExpression_ : +>    `while` [_Expression_]except struct expression [_BlockExpression_] + A `while` loop begins by evaluating the boolean loop conditional expression. If the loop conditional expression evaluates to `true`, the loop body block executes, then control returns to the loop conditional expression. If the loop @@ -44,9 +67,14 @@ while i < 10 { ## `while let` loops +> **Syntax** +> [_WhileLetExpression_] : +>    `while` `let` _Pattern_ `=` [_Expression_]except struct expression +> [_BlockExpression_] + A `while let` loop is semantically similar to a `while` loop but in place of a condition expression it expects the keyword `let` followed by a refutable -pattern, an `=` and an expression. If the value of the expression on the right +pattern, an `=` and a block expression. If the value of the expression on the right hand side of the `=` matches the pattern, the loop body block executes then control returns to the pattern matching statement. Otherwise, the while expression completes. @@ -61,6 +89,11 @@ while let Some(y) = x.pop() { ## Iterator loops +> **Syntax** +> _IteratorLoopExpression_ : +>    `for` _Pattern_ `in` [_Expression_]except struct expression +> [_BlockExpression_] + A `for` expression is a syntactic construct for looping over elements provided by an implementation of `std::iter::IntoIterator`. If the iterator yields a value, that value is given the specified name and the body of the loop is @@ -89,6 +122,10 @@ assert_eq!(sum, 55); ## Loop labels +> **Syntax** +> _LoopLabel_ : +>    [LIFETIME_OR_LABEL] `:` + A loop expression may optionally have a _label_. The label is written as a lifetime preceding the loop expression, as in `'foo: loop { break 'foo; }`, `'bar: while false {}`, `'humbug: for _ in 0..0 {}`. @@ -99,6 +136,10 @@ expressions](#continue-expressions). ## `break` expressions +> **Syntax** +> _BreakExpression_ : +>    `break` [LIFETIME_OR_LABEL]? [_Expression_]? + When `break` is encountered, execution of the associated loop body is immediately terminated, for example: @@ -131,6 +172,10 @@ the forms `break`, `break 'label` or ([see below](#break-and-loop-values)) ## `continue` expressions +> **Syntax** +> _ContinueExpression_ : +>    `continue` [LIFETIME_OR_LABEL]? + When `continue` is encountered, the current iteration of the associated loop body is immediately terminated, returning control to the loop *head*. In the case of a `while` loop, the head is the conditional expression controlling @@ -165,3 +210,10 @@ In the case a `loop` has an associated `break`, it is not considered diverging, and the `loop` must have a type compatible with each `break` expression. `break` without an expression is considered identical to `break` with expression `()`. + +[IDENTIFIER]: identifiers.html + +[_Expression_]: expressions.html +[_BlockExpression_]: expressions/block-expr.html + +[LIFETIME_OR_LABEL]: tokens.html#symbols From 67b0c0b6fdc936012635fd67236e4321fef90ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Sat, 14 Oct 2017 14:56:58 -0300 Subject: [PATCH 2/3] Fixed the confusion around expression and the block expression of while let --- src/expressions/loop-expr.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/expressions/loop-expr.md b/src/expressions/loop-expr.md index bc99b7d5c..4a62b6505 100644 --- a/src/expressions/loop-expr.md +++ b/src/expressions/loop-expr.md @@ -74,8 +74,8 @@ while i < 10 { A `while let` loop is semantically similar to a `while` loop but in place of a condition expression it expects the keyword `let` followed by a refutable -pattern, an `=` and a block expression. If the value of the expression on the right -hand side of the `=` matches the pattern, the loop body block executes then +pattern, an `=`, an expression and a block expression. If the value of the expression on +the right hand side of the `=` matches the pattern, the loop body block executes then control returns to the pattern matching statement. Otherwise, the while expression completes. From 413fe212fea8311303af9bc8eadac2c096895151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=A1ulio=20Bezerra?= Date: Wed, 29 Nov 2017 23:01:51 -0300 Subject: [PATCH 3/3] Rename "while let loop" section and production to "predicate pattern loop". Thanks Havvy. --- src/expressions/loop-expr.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/expressions/loop-expr.md b/src/expressions/loop-expr.md index 4a62b6505..4b296f3b3 100644 --- a/src/expressions/loop-expr.md +++ b/src/expressions/loop-expr.md @@ -5,21 +5,21 @@ >    [_LoopLabel_]? ( >          [_InfiniteLoopExpression_] >       | [_PredicateLoopExpression_] +>       | [_PredicatePatternLoopExpression_] >       | [_IteratorLoopExpression_] ->       | [_WhileLetExpression_] >    ) [_LoopLabel_]: #loop-labels [_InfiniteLoopExpression_]: #infinite-loops [_PredicateLoopExpression_]: #predicate-loops +[_PredicatePatternLoopExpression_]: #predicate-pattern-loops [_IteratorLoopExpression_]: #iterator-loops -[_WhileLetExpression_]: #while-let-loops Rust supports four 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](#while-let-loops) tests a refutable pattern. +* A [`while let` expression](#predicate-pattern-loops) tests a refutable pattern. * A [`for` expression](#iterator-loops) extracts values from an iterator, looping until the iterator is empty. @@ -65,10 +65,10 @@ while i < 10 { } ``` -## `while let` loops +## Predicate pattern loops > **Syntax** -> [_WhileLetExpression_] : +> [_PredicatePatternLoopExpression_] : >    `while` `let` _Pattern_ `=` [_Expression_]except struct expression > [_BlockExpression_]