Skip to content

Commit 515f88f

Browse files
committed
manual: rewrite for-expression section to reflect this year's definition.
1 parent fd85a00 commit 515f88f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

doc/rust.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,13 +2120,21 @@ do f |j| {
21202120
### For expressions
21212121

21222122
~~~~~~~~{.ebnf .gram}
2123-
for_expr : "for" pat "in" expr '{' block '}' ;
2123+
for_expr : "for" expr [ '|' ident_list '|' ] ? '{' block '}' ;
21242124
~~~~~~~~
21252125

2126-
A _for loop_ is controlled by a vector or string. The for loop bounds-checks
2127-
the underlying sequence *once* when initiating the loop, then repeatedly
2128-
executes the loop body with the loop variable referencing the successive
2129-
elements of the underlying sequence, one iteration per sequence element.
2126+
A _for expression_ is similar to a [`do` expression](#do-expressions),
2127+
in that it provides a special block-form of lambda expression,
2128+
suited to passing the `block` function to a higher-order function implementing a loop.
2129+
2130+
Like a `do` expression, a `return` expression inside a `for` expresison is rewritten,
2131+
to access a local flag that causes an early return in the caller.
2132+
2133+
Additionally, [`break`](#break-expressions) and [`loop`](#loop-expressions) expressions
2134+
are rewritten inside `for` expressions, with a combination of local flag variables,
2135+
and early boolean-valued returns from the `block` function,
2136+
such that the meaning of `break` and `loop` is preserved in a primitive loop
2137+
when rewritten as a `for` loop controlled by a higher order function.
21302138

21312139
An example a for loop:
21322140

@@ -2135,7 +2143,7 @@ An example a for loop:
21352143
# fn bar(f: foo) { }
21362144
# let a = 0, b = 0, c = 0;
21372145
2138-
let v: ~[foo] = ~[a, b, c];
2146+
let v: [foo] = [a, b, c];
21392147
21402148
for v.each |e| {
21412149
bar(*e);

0 commit comments

Comments
 (0)