Skip to content

Commit dd923e3

Browse files
committed
tutorial: fix for-loop example
Although in the example function `each` works as expected with rust-0.6 (the latest release), it fails to even compile with `incoming` rust (see test/compile-fail/bad-for-loop-2.rs). Change the function to return a `bool` instead of `()`: this works fine with both versions of rust, and does not misguide potential contributors. Signed-off-by: Ramkumar Ramachandra <[email protected]>
1 parent c2cb238 commit dd923e3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

doc/tutorial.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1613,18 +1613,19 @@ loop. Like `do`, `for` is a nice syntax for describing control flow
16131613
with closures. Additionally, within a `for` loop, `break`, `loop`,
16141614
and `return` work just as they do with `while` and `loop`.
16151615

1616-
Consider again our `each` function, this time improved to
1617-
break early when the iteratee returns `false`:
1616+
Consider again our `each` function, this time improved to return
1617+
immediately when the iteratee returns `false`:
16181618

16191619
~~~~
1620-
fn each(v: &[int], op: &fn(v: &int) -> bool) {
1620+
fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
16211621
let mut n = 0;
16221622
while n < v.len() {
16231623
if !op(&v[n]) {
1624-
break;
1624+
return false;
16251625
}
16261626
n += 1;
16271627
}
1628+
return true;
16281629
}
16291630
~~~~
16301631

0 commit comments

Comments
 (0)