Skip to content

Commit b31501d

Browse files
committed
---
yaml --- r: 12513 b: refs/heads/master c: 73ea690 h: refs/heads/master i: 12511: eb8d362 v: v3
1 parent 95c2101 commit b31501d

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 054a312a33403c6f28b2948ac7ba25dec7f3b267
2+
refs/heads/master: 73ea690016452d7c7d732d0fd6940d6271411606
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/doc/tutorial.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ mode for your favorite editor, let us know so that we can link to it.
183183
Assuming you've programmed in any C-family language (C++, Java,
184184
JavaScript, C#, or PHP), Rust will feel familiar. The main surface
185185
difference to be aware of is that the bodies of `if` statements and of
186-
loops *have* to be wrapped in brackets. Single-statement, bracket-less
187-
bodies are not allowed.
186+
`while` loops *have* to be wrapped in brackets. Single-statement,
187+
bracket-less bodies are not allowed.
188188

189189
If the verbosity of that bothers you, consider the fact that this
190190
allows you to omit the parentheses around the condition in `if`,
@@ -690,9 +690,9 @@ do {
690690
} while any_cake_left();
691691
~~~~
692692

693-
For more involved iteration, such as going over the elements of a hash
694-
table, Rust uses higher-order functions. We'll come back to those in a
695-
moment.
693+
For more involved iteration, such as going over the elements of a
694+
collection, Rust uses higher-order functions. We'll come back to those
695+
in a moment.
696696

697697
## Failure
698698

@@ -952,6 +952,49 @@ for_rev([1, 2, 3]) {|n|
952952
Note that, because `for_rev()` returns unit type, no semicolon is
953953
needed when the final closure is pulled outside of the parentheses.
954954

955+
# For loops
956+
957+
To allow breaking out of loops, many iteration functions, such as
958+
`vec::each`, take a function that returns a boolean, and can return
959+
`false` to break off iteration.
960+
961+
~~~~
962+
vec::each([2, 4, 8, 5, 16]) {|n|
963+
if n % 2 != 0 {
964+
io::println("found odd number!");
965+
false
966+
} else { true }
967+
}
968+
~~~~
969+
970+
You can see how that gets noisy. As a syntactic convenience, if the
971+
call is preceded by the keyword `for`, the block will implicitly
972+
return `true`, and `break` and `cont` can be used, much like in a
973+
`while` loop, to explicitly return `false` or `true`.
974+
975+
~~~~
976+
for vec::each([2, 4, 8, 5, 16]) {|n|
977+
if n % 2 != 0 {
978+
io::println("found odd number!");
979+
break;
980+
}
981+
}
982+
~~~~
983+
984+
As an added bonus, you can use the `ret` keyword, which is not
985+
normally allowed in blocks, in a block that appears as the body of a
986+
`for` loop — this will cause a return to happen from the outer
987+
function, not just the loop body.
988+
989+
~~~~
990+
fn contains(v: [int], elt: int) -> bool {
991+
for vec::each(v) {|x|
992+
if (x == elt) { ret true; }
993+
}
994+
false
995+
}
996+
~~~~
997+
955998
# Datatypes
956999

9571000
Rust datatypes are, by default, immutable. The core datatypes of Rust

0 commit comments

Comments
 (0)