]> git.lizzy.rs Git - rust.git/commitdiff
copyediting: while loops
authorSteve Klabnik <steve@steveklabnik.com>
Fri, 10 Apr 2015 16:25:40 +0000 (12:25 -0400)
committerSteve Klabnik <steve@steveklabnik.com>
Fri, 10 Apr 2015 16:26:58 +0000 (12:26 -0400)
src/doc/trpl/while-loops.md

index e1fe9b589b3f6b296990c4b8250f8e20920c578b..f2e2f6b6f49a7403048a36ff755ac18cf8119fc1 100644 (file)
@@ -1,7 +1,6 @@
 % while loops
 
-The other kind of looping construct in Rust is the `while` loop. It looks like
-this:
+Rust also has a `while` loop. It looks like this:
 
 ```{rust}
 let mut x = 5; // mut x: u32
@@ -9,45 +8,52 @@ let mut done = false; // mut done: bool
 
 while !done {
     x += x - 3;
+
     println!("{}", x);
-    if x % 5 == 0 { done = true; }
+
+    if x % 5 == 0 {
+        done = true;
+    }
 }
 ```
 
-`while` loops are the correct choice when you're not sure how many times
+`while` loops are the correct choice when youre not sure how many times
 you need to loop.
 
 If you need an infinite loop, you may be tempted to write this:
 
-```{rust,ignore}
+```rust,ignore
 while true {
 ```
 
 However, Rust has a dedicated keyword, `loop`, to handle this case:
 
-```{rust,ignore}
+```rust,ignore
 loop {
 ```
 
-Rust's control-flow analysis treats this construct differently than a
-`while true`, since we know that it will always loop. The details of what
-that _means_ aren't super important to understand at this stage, but in
-general, the more information we can give to the compiler, the better it
-can do with safety and code generation, so you should always prefer
-`loop` when you plan to loop infinitely.
+Rust’s control-flow analysis treats this construct differently than a `while
+true`, since we know that it will always loop. In general, the more information
+we can give to the compiler, the better it can do with safety and code
+generation, so you should always prefer `loop` when you plan to loop
+infinitely.
 
 ## Ending iteration early
 
-Let's take a look at that `while` loop we had earlier:
+Lets take a look at that `while` loop we had earlier:
 
-```{rust}
+```rust
 let mut x = 5;
 let mut done = false;
 
 while !done {
     x += x - 3;
+
     println!("{}", x);
-    if x % 5 == 0 { done = true; }
+
+    if x % 5 == 0 {
+        done = true;
+    }
 }
 ```
 
@@ -57,12 +63,14 @@ modifying iteration: `break` and `continue`.
 
 In this case, we can write the loop in a better way with `break`:
 
-```{rust}
+```rust
 let mut x = 5;
 
 loop {
     x += x - 3;
+
     println!("{}", x);
+
     if x % 5 == 0 { break; }
 }
 ```
@@ -72,7 +80,7 @@ We now loop forever with `loop` and use `break` to break out early.
 `continue` is similar, but instead of ending the loop, goes to the next
 iteration. This will only print the odd numbers:
 
-```{rust}
+```rust
 for x in 0..10 {
     if x % 2 == 0 { continue; }
 
@@ -80,4 +88,6 @@ for x in 0..10 {
 }
 ```
 
-Both `continue` and `break` are valid in both kinds of loops.
+Both `continue` and `break` are valid in both `while` loops and [`for` loops][for].
+
+[for]: for-loops.html