@@ -2459,12 +2459,25 @@ do k(3) |j| {
2459
2459
### For expressions
2460
2460
2461
2461
~~~~~~~~ {.ebnf .gram}
2462
- for_expr : "for" pat "in" expr '{' block '}' ;
2462
+ for_expr : "for" expr [ '|' ident_list '|' ] ? '{' block '}' ;
2463
2463
~~~~~~~~
2464
2464
2465
- A ` for ` expression is a syntactic construct for looping
2466
- over elements provided by an implementation of
2467
- ` std::iterator::Iterator ` .
2465
+ A _ for expression_ is similar to a [ ` do ` expression] ( #do-expressions ) ,
2466
+ in that it provides a special block-form of lambda expression,
2467
+ suited to passing the ` block ` function to a higher-order function implementing a loop.
2468
+
2469
+ In contrast to a ` do ` expression, a ` for ` expression is designed to work
2470
+ with methods such as ` each ` and ` times ` , that require the body block to
2471
+ return a boolean. The ` for ` expression accommodates this by implicitly
2472
+ returning ` true ` at the end of each block, unless a ` break ` expression
2473
+ is evaluated.
2474
+
2475
+ In addition, [ ` break ` ] ( #break-expressions ) and [ ` loop ` ] ( #loop-expressions ) expressions
2476
+ are rewritten inside ` for ` expressions in the same way that ` return ` expressions are,
2477
+ with a combination of local flag variables,
2478
+ and early boolean-valued returns from the ` block ` function,
2479
+ such that the meaning of ` break ` and ` loop ` is preserved in a primitive loop
2480
+ when rewritten as a ` for ` loop controlled by a higher order function.
2468
2481
2469
2482
An example of a for loop over the contents of a vector:
2470
2483
0 commit comments