Skip to content

Commit b71a1ec

Browse files
committed
added and rewrote text for 'do' and 'for' loops
1 parent 999f692 commit b71a1ec

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

doc/rust.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,14 @@ do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ;
22502250
A _do expression_ provides a more-familiar block-syntax for a [lambda expression](#lambda-expressions),
22512251
including a special translation of [return expressions](#return-expressions) inside the supplied block.
22522252

2253+
Any occurrence of a [return expression](#return-expressions)
2254+
inside this `block` expression is rewritten
2255+
as a reference to an (anonymous) flag set in the caller's environment,
2256+
which is checked on return from the `expr` and, if set,
2257+
causes a corresponding return from the caller.
2258+
In this way, the meaning of `return` statements in language built-in control blocks is preserved,
2259+
if they are rewritten using lambda functions and `do` expressions as abstractions.
2260+
22532261
The optional `ident_list` and `block` provided in a `do` expression are parsed as though they constitute a lambda expression;
22542262
if the `ident_list` is missing, an empty `ident_list` is implied.
22552263

@@ -2296,19 +2304,15 @@ A _for expression_ is similar to a [`do` expression](#do-expressions),
22962304
in that it provides a special block-form of lambda expression,
22972305
suited to passing the `block` function to a higher-order function implementing a loop.
22982306

2299-
Like a `do` expression, a `return` expression inside a `for` expresison is rewritten,
2300-
to access a local flag that causes an early return in the caller.
2301-
2302-
Additionally, any occurrence of a [return expression](#return-expressions)
2303-
inside the `block` of a `for` expression is rewritten
2304-
as a reference to an (anonymous) flag set in the caller's environment,
2305-
which is checked on return from the `expr` and, if set,
2306-
causes a corresponding return from the caller.
2307-
In this way, the meaning of `return` statements in language built-in control blocks is preserved,
2308-
if they are rewritten using lambda functions and `do` expressions as abstractions.
2307+
In contrast to a `do` expression, a `for` expression is designed to work
2308+
with methods such as `each` and `times`, that require the body block to
2309+
return a boolean. The `for` expression accommodates this by implicitly
2310+
returning `true` at the end of each block, unless a `break` expression
2311+
is evaluated.
23092312

2310-
Like `return` expressions, any [`break`](#break-expressions) and [`loop`](#loop-expressions) expressions
2311-
are rewritten inside `for` expressions, with a combination of local flag variables,
2313+
In addition, [`break`](#break-expressions) and [`loop`](#loop-expressions) expressions
2314+
are rewritten inside `for` expressions in the same way that `return` expressions are,
2315+
with a combination of local flag variables,
23122316
and early boolean-valued returns from the `block` function,
23132317
such that the meaning of `break` and `loop` is preserved in a primitive loop
23142318
when rewritten as a `for` loop controlled by a higher order function.

0 commit comments

Comments
 (0)