about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-02-20 10:36:49 -0800
committerbors <bors@rust-lang.org>2014-02-20 10:36:49 -0800
commit6532d2fa0d173e4f815ac2144ff9860f5343cd7d (patch)
tree928e6d381d88eeabe36aaa5be487b9680f900815
parent1366e04cf48a3274571a4ae88f9700834ffd58c5 (diff)
parent68c960a8bb6b29c7e5e81ee4cefd8d135984b512 (diff)
auto merge of #12161 : aepsil0n/rust/docs/for-loop, r=alexcrichton
I just started learning Rust and the absence of an explanation of the for-loop in the beginning really bugged me about the tutorial. Hence I simply added these lines, where I would have expected them. I know that there is something later on in the section on traits. However, this simple iteration scheme feels like something that you should be aware of right away.
-rw-r--r--src/doc/tutorial.md26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/doc/tutorial.md b/src/doc/tutorial.md
index 094771897f4..6ae8c1d704d 100644
--- a/src/doc/tutorial.md
+++ b/src/doc/tutorial.md
@@ -582,6 +582,32 @@ loop {
 This code prints out a weird sequence of numbers and stops as soon as
 it finds one that can be divided by five.
 
+There is also a for-loop that can be used to iterate over a range of numbers:
+
+~~~~
+for n in range(0, 5) {
+    println!("{}", n);
+}
+~~~~
+
+The snippet above prints integer numbers under 5 starting at 0.
+
+More generally, a for loop works with anything implementing the `Iterator` trait.
+Data structures can provide one or more methods that return iterators over
+their contents. For example, strings support iteration over their contents in
+various ways:
+
+~~~~
+let s = "Hello";
+for c in s.chars() {
+    println!("{}", c);
+}
+~~~~
+
+The snippet above prints the characters in "Hello" vertically, adding a new
+line after each character.
+
+
 # Data structures
 
 ## Structs