@@ -2120,13 +2120,21 @@ do f |j| {
2120
2120
### For expressions
2121
2121
2122
2122
~~~~~~~~ {.ebnf .gram}
2123
- for_expr : "for" pat "in" expr '{' block '}' ;
2123
+ for_expr : "for" expr [ '|' ident_list '|' ] ? '{' block '}' ;
2124
2124
~~~~~~~~
2125
2125
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.
2130
2138
2131
2139
An example a for loop:
2132
2140
@@ -2135,7 +2143,7 @@ An example a for loop:
2135
2143
# fn bar(f: foo) { }
2136
2144
# let a = 0, b = 0, c = 0;
2137
2145
2138
- let v: ~ [foo] = ~ [a, b, c];
2146
+ let v: [foo] = [a, b, c];
2139
2147
2140
2148
for v.each |e| {
2141
2149
bar(*e);
0 commit comments