Skip to content

Commit

Permalink
tutorial: fix for-loop example
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
artagnon committed Jun 6, 2013
1 parent c2cb238 commit dd923e3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions doc/tutorial.md
Expand Up @@ -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`,
and `return` work just as they do with `while` and `loop`.

Consider again our `each` function, this time improved to
break early when the iteratee returns `false`:
Consider again our `each` function, this time improved to return
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;
while n < v.len() {
if !op(&v[n]) {
break;
return false;
}
n += 1;
}
return true;
}
~~~~

Expand Down

5 comments on commit dd923e3

@bors
Copy link
Contributor

@bors bors commented on dd923e3 Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on dd923e3 Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging artagnon/rust/bad-for-loop = dd923e3 into auto

@bors
Copy link
Contributor

@bors bors commented on dd923e3 Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

artagnon/rust/bad-for-loop = dd923e3 merged ok, testing candidate = 8450dc8

@bors
Copy link
Contributor

@bors bors commented on dd923e3 Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on dd923e3 Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 8450dc8

Please sign in to comment.