mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
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 <artagnon@gmail.com>
This commit is contained in:
parent
c2cb238075
commit
dd923e3831
@ -1613,18 +1613,19 @@ loop. Like `do`, `for` is a nice syntax for describing control flow
|
|||||||
with closures. Additionally, within a `for` loop, `break`, `loop`,
|
with closures. Additionally, within a `for` loop, `break`, `loop`,
|
||||||
and `return` work just as they do with `while` and `loop`.
|
and `return` work just as they do with `while` and `loop`.
|
||||||
|
|
||||||
Consider again our `each` function, this time improved to
|
Consider again our `each` function, this time improved to return
|
||||||
break early when the iteratee returns `false`:
|
immediately when the iteratee returns `false`:
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
fn each(v: &[int], op: &fn(v: &int) -> bool) {
|
fn each(v: &[int], op: &fn(v: &int) -> bool) -> bool {
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
while n < v.len() {
|
while n < v.len() {
|
||||||
if !op(&v[n]) {
|
if !op(&v[n]) {
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
n += 1;
|
n += 1;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user