Skip to content

Commit af9cc11

Browse files
committed
---
yaml --- r: 35895 b: refs/heads/try2 c: fd85a00 h: refs/heads/master i: 35893: 107077a 35891: e0ac8e2 35887: 83903ca v: v3
1 parent 63f8845 commit af9cc11

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 693866d75e25425053c61dc16adcc12b977d86f0
8+
refs/heads/try2: fd85a0000a542a52f0fda3bd2f941fba452e0698
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/doc/rust.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,52 @@ the loop.
20712071
A `loop` expression is only permitted in the body of a loop.
20722072

20732073

2074+
### Do expressions
2075+
2076+
~~~~~~~~{.ebnf .gram}
2077+
do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ;
2078+
~~~~~~~~
2079+
2080+
A _do expression_ provides a more-familiar block-syntax for a [lambda expression](#lambda-expressions),
2081+
including a special translation of [return expressions](#return-expressions) inside the supplied block.
2082+
2083+
The optional `ident_list` and `block` provided in a `do` expression are parsed as though they constitute a lambda expression;
2084+
if the `ident_list` is missing, an empty `ident_list` is implied.
2085+
2086+
The lambda expression is then provided as a _trailing argument_
2087+
to the outermost [call](#call-expressions) or [method call](#method-call-expressions) expression
2088+
in the `expr` following `do`.
2089+
If the `expr` is a [path expression](#path-expressions), it is parsed as though it is a call expression.
2090+
If the `expr` is a [field expression](#field-expressions), it is parsed as though it is a method call expression.
2091+
2092+
Additionally, any occurrence of a [return expression](#return-expressions)
2093+
inside the `block` of a `do` expression is rewritten
2094+
as a reference to an (anonymous) flag set in the caller's environment,
2095+
which is checked on return from the `expr` and, if set,
2096+
causes a corresponding return from the caller.
2097+
In this way, the meaning of `return` statements in language built-in control blocks is preserved,
2098+
if they are rewritten using lambda functions and `do` expressions as abstractions.
2099+
2100+
Therefore the two calls to `f` in this example are equivalent.
2101+
Both cause an early return from the caller's frame:
2102+
2103+
~~~~
2104+
# fn f(f: fn(int)) { }
2105+
# fn g(i: int) { }
2106+
2107+
{
2108+
let mut _early_ret = false;
2109+
f(|j| { g(j); _early_ret = true; });
2110+
if early_ret { return; }
2111+
}
2112+
2113+
do f |j| {
2114+
g(j);
2115+
return;
2116+
}
2117+
~~~~
2118+
2119+
20742120
### For expressions
20752121

20762122
~~~~~~~~{.ebnf .gram}

0 commit comments

Comments
 (0)