From 6e2f18ef322c094ed49eee11994fc49c5cacbc2a Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sun, 31 May 2015 14:03:57 +0200 Subject: [PATCH 1/4] Add loopcounter section to the for-loop chapter Sometimes loop counters are useful and we should show new users how it is achieved in Rust. --- src/doc/trpl/for-loops.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/doc/trpl/for-loops.md b/src/doc/trpl/for-loops.md index 1e3f2fa54bcc6..c8422f351d13b 100644 --- a/src/doc/trpl/for-loops.md +++ b/src/doc/trpl/for-loops.md @@ -41,3 +41,38 @@ so our loop will print `0` through `9`, not `10`. Rust does not have the “C-style” `for` loop on purpose. Manually controlling each element of the loop is complicated and error prone, even for experienced C developers. + +# Loopcounter + +When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. + +#### On ranges: + +```rust +for (i,j) in (5..10).enumerate() { + println!("i = {} and j = {}", i, j); +} +``` +Outputs: +``` +i = 0 and j = 5 +i = 1 and j = 6 +i = 2 and j = 7 +i = 3 and j = 8 +i = 4 and j = 9 +``` +Don't forget to add the parentheses around the range. + +#### On iterators: +```rust +for (linenumber, line) in lines.enumerate() { + println!("{}: {}", linenumber, line); +} +``` +Outputs: +``` +0: Content of line one +1: Content of line two +2: Content of line tree +3: Content of line four +``` From 151c3d3644c2d8b21465dd8a48ad753539e18c88 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sun, 31 May 2015 20:56:35 +0200 Subject: [PATCH 2/4] Corrected some formatting issues --- src/doc/trpl/for-loops.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/doc/trpl/for-loops.md b/src/doc/trpl/for-loops.md index c8422f351d13b..9cd1689cf3dea 100644 --- a/src/doc/trpl/for-loops.md +++ b/src/doc/trpl/for-loops.md @@ -42,35 +42,41 @@ Rust does not have the “C-style” `for` loop on purpose. Manually controlling each element of the loop is complicated and error prone, even for experienced C developers. -# Loopcounter +# Enumerate When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. -#### On ranges: +## On ranges: ```rust for (i,j) in (5..10).enumerate() { println!("i = {} and j = {}", i, j); } ``` + Outputs: -``` + +```rust i = 0 and j = 5 i = 1 and j = 6 i = 2 and j = 7 i = 3 and j = 8 i = 4 and j = 9 ``` + Don't forget to add the parentheses around the range. -#### On iterators: +## On iterators: + ```rust for (linenumber, line) in lines.enumerate() { println!("{}: {}", linenumber, line); } ``` + Outputs: -``` + +```rust 0: Content of line one 1: Content of line two 2: Content of line tree From 90057c29623a945ada73d7156288057e194ef962 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sun, 31 May 2015 23:37:22 +0200 Subject: [PATCH 3/4] output code language to text --- src/doc/trpl/for-loops.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/trpl/for-loops.md b/src/doc/trpl/for-loops.md index 9cd1689cf3dea..79720a31740e7 100644 --- a/src/doc/trpl/for-loops.md +++ b/src/doc/trpl/for-loops.md @@ -56,7 +56,7 @@ for (i,j) in (5..10).enumerate() { Outputs: -```rust +```text i = 0 and j = 5 i = 1 and j = 6 i = 2 and j = 7 @@ -76,7 +76,7 @@ for (linenumber, line) in lines.enumerate() { Outputs: -```rust +```text 0: Content of line one 1: Content of line two 2: Content of line tree From c4f42a1ab4cf047921018d6ecabbb00ea47fc683 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Mon, 1 Jun 2015 21:34:31 +0200 Subject: [PATCH 4/4] Added # let lines = "hello\nworld".lines(); to pass the tests --- src/doc/trpl/for-loops.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/doc/trpl/for-loops.md b/src/doc/trpl/for-loops.md index 79720a31740e7..2866cee3a1a63 100644 --- a/src/doc/trpl/for-loops.md +++ b/src/doc/trpl/for-loops.md @@ -69,6 +69,7 @@ Don't forget to add the parentheses around the range. ## On iterators: ```rust +# let lines = "hello\nworld".lines(); for (linenumber, line) in lines.enumerate() { println!("{}: {}", linenumber, line); }