Skip to content

Commit 9bbff50

Browse files
committed
manual: fix various examples.
1 parent 089e257 commit 9bbff50

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

doc/rust.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,30 +2089,16 @@ in the `expr` following `do`.
20892089
If the `expr` is a [path expression](#path-expressions), it is parsed as though it is a call expression.
20902090
If the `expr` is a [field expression](#field-expressions), it is parsed as though it is a method call expression.
20912091

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:
2092+
In this example, both calls to `f` are equivalent:
21022093

21032094
~~~~
21042095
# fn f(f: fn(int)) { }
21052096
# fn g(i: int) { }
21062097
2107-
{
2108-
let mut _early_ret = false;
2109-
f(|j| { g(j); _early_ret = true; });
2110-
if _early_ret { return; }
2111-
}
2098+
f(|j| g(j));
21122099
21132100
do f |j| {
21142101
g(j);
2115-
return;
21162102
}
21172103
~~~~
21182104

@@ -2130,7 +2116,15 @@ suited to passing the `block` function to a higher-order function implementing a
21302116
Like a `do` expression, a `return` expression inside a `for` expresison is rewritten,
21312117
to access a local flag that causes an early return in the caller.
21322118

2133-
Additionally, [`break`](#break-expressions) and [`loop`](#loop-expressions) expressions
2119+
Additionally, any occurrence of a [return expression](#return-expressions)
2120+
inside the `block` of a `for` expression is rewritten
2121+
as a reference to an (anonymous) flag set in the caller's environment,
2122+
which is checked on return from the `expr` and, if set,
2123+
causes a corresponding return from the caller.
2124+
In this way, the meaning of `return` statements in language built-in control blocks is preserved,
2125+
if they are rewritten using lambda functions and `do` expressions as abstractions.
2126+
2127+
Like `return` expressions, any [`break`](#break-expressions) and [`loop`](#loop-expressions) expressions
21342128
are rewritten inside `for` expressions, with a combination of local flag variables,
21352129
and early boolean-valued returns from the `block` function,
21362130
such that the meaning of `break` and `loop` is preserved in a primitive loop
@@ -2143,7 +2137,7 @@ An example a for loop:
21432137
# fn bar(f: foo) { }
21442138
# let a = 0, b = 0, c = 0;
21452139
2146-
let v: [foo] = [a, b, c];
2140+
let v: &[foo] = &[a, b, c];
21472141
21482142
for v.each |e| {
21492143
bar(*e);
@@ -2530,7 +2524,7 @@ The kind of a vector type depends on the kind of its member type, as with other
25302524
An example of a vector type and its use:
25312525

25322526
~~~~
2533-
let v: &[int] = [7, 5, 3];
2527+
let v: &[int] = &[7, 5, 3];
25342528
let i: int = v[2];
25352529
assert (i == 3);
25362530
~~~~
@@ -2701,16 +2695,16 @@ trait Printable {
27012695
fn to_str() -> ~str;
27022696
}
27032697
2704-
impl ~str: Printable {
2705-
fn to_str() -> ~str { self }
2698+
impl int: Printable {
2699+
fn to_str() -> ~str { int::to_str(self, 10) }
27062700
}
27072701
2708-
fn print(a: Printable) {
2702+
fn print(a: @Printable) {
27092703
io::println(a.to_str());
27102704
}
27112705
27122706
fn main() {
2713-
print(~"meow" as ~Printable);
2707+
print(@10 as @Printable);
27142708
}
27152709
~~~~~~~~
27162710

0 commit comments

Comments
 (0)