Skip to content

Commit f943667

Browse files
committed
Remove do ... while loops from the tests and docs.
1 parent 13c924c commit f943667

15 files changed

+45
-79
lines changed

doc/rust.md

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,28 +1991,19 @@ way.
19911991

19921992
*TODO*.
19931993

1994-
### While expressions
1994+
### While loops
19951995

19961996
~~~~~~~~{.ebnf .gram}
19971997
while_expr : "while" expr '{' block '}'
19981998
| "do" '{' block '}' "while" expr ;
19991999
~~~~~~~~
20002000

2001-
A `while` expression is a loop construct. A `while` loop may be either a
2002-
simple `while` or a `do`-`while` loop.
2001+
A `while` loop begins by evaluating the boolean loop conditional expression.
2002+
If the loop conditional expression evaluates to `true`, the loop body block
2003+
executes and control returns to the loop conditional expression. If the loop
2004+
conditional expression evaluates to `false`, the `while` expression completes.
20032005

2004-
In the case of a simple `while`, the loop begins by evaluating the boolean
2005-
loop conditional expression. If the loop conditional expression evaluates to
2006-
`true`, the loop body block executes and control returns to the loop
2007-
conditional expression. If the loop conditional expression evaluates to
2008-
`false`, the `while` expression completes.
2009-
2010-
In the case of a `do`-`while`, the loop begins with an execution of the loop
2011-
body. After the loop body executes, it evaluates the loop conditional
2012-
expression. If it evaluates to `true`, control returns to the beginning of the
2013-
loop body. If it evaluates to `false`, control exits the loop.
2014-
2015-
An example of a simple `while` expression:
2006+
An example:
20162007

20172008
~~~~
20182009
# let mut i = 0;
@@ -2024,18 +2015,6 @@ while i < 10 {
20242015
}
20252016
~~~~
20262017

2027-
An example of a `do`-`while` expression:
2028-
2029-
~~~~
2030-
# let mut i = 0;
2031-
# let println = io::println;
2032-
2033-
do {
2034-
println("hello\n");
2035-
i = i + 1;
2036-
} while i < 10;
2037-
~~~~
2038-
20392018
### Infinite loops
20402019

20412020
A `loop` expression denotes an infinite loop:

doc/tutorial.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,15 @@ a specific value, are not allowed.
667667
keyword `break` can be used to abort the loop, and `cont` can be used
668668
to abort the current iteration and continue with the next.
669669

670+
~~~~
671+
let mut cake_amount = 8;
672+
while cake_amount > 0 {
673+
cake_amount -= 1;
674+
}
675+
~~~~
676+
677+
`loop` is the preferred way of writing `while true`:
678+
670679
~~~~
671680
let mut x = 5;
672681
while true {
@@ -679,17 +688,6 @@ while true {
679688
This code prints out a weird sequence of numbers and stops as soon as
680689
it finds one that can be divided by five.
681690

682-
There's also `while`'s ugly cousin, `do`/`while`, which does not check
683-
its condition on the first iteration, using traditional syntax:
684-
685-
~~~~
686-
# fn eat_cake() {}
687-
# fn any_cake_left() -> bool { false }
688-
do {
689-
eat_cake();
690-
} while any_cake_left();
691-
~~~~
692-
693691
For more involved iteration, such as going over the elements of a
694692
collection, Rust uses higher-order functions. We'll come back to those
695693
in a moment.
@@ -2496,12 +2494,12 @@ Here is the function which implements the child task:
24962494
fn stringifier(from_parent: comm::port<uint>,
24972495
to_parent: comm::chan<str>) {
24982496
let mut value: uint;
2499-
do {
2497+
loop {
25002498
value = comm::recv(from_parent);
25012499
comm::send(to_parent, uint::to_str(value, 10u));
2502-
} while value != 0u;
2500+
if value == 0u { break; }
2501+
}
25032502
}
2504-
25052503
~~~~
25062504

25072505
You can see that the function takes two parameters. The first is a

src/test/compile-fail/borrowck-lend-flow.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,6 @@ fn while_aliased_mut_cond(cond: bool, cond2: bool) {
6161
}
6262
}
6363

64-
fn do_while_aliased_mut(cond: bool) {
65-
let mut v = ~3, w = ~4;
66-
let mut _x = &mut w;
67-
do {
68-
borrow(v); //! ERROR loan of mutable local variable as immutable conflicts with prior loan
69-
_x = &mut v; //! NOTE prior loan as mutable granted here
70-
} while cond;
71-
}
72-
73-
fn loop_in_block() {
74-
let mut v = ~3, w = ~4;
75-
let mut _x = &mut w;
76-
uint::range(0u, 10u) {|_i|
77-
borrow(v); //! ERROR loan of mutable local variable as immutable conflicts with prior loan
78-
_x = &mut v; //! NOTE prior loan as mutable granted here
79-
}
80-
}
81-
8264
fn at_most_once_block() {
8365
fn at_most_once(f: fn()) { f() }
8466

src/test/compile-fail/break-uninit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn foo() -> int {
44
let x: int;
55
let i: int;
66

7-
do { i = 0; break; x = 0; } while x != 0
7+
loop { i = 0; break; x = 0; }
88

99
log(debug, x);
1010

src/test/compile-fail/break-uninit2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn foo() -> int {
44
let x: int;
55
let i: int;
66

7-
do { i = 0; break; x = 0; } while 1 != 2
7+
while 1 != 2 { i = 0; break; x = 0; }
88

99
log(debug, x);
1010

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
// xfail-test
2+
// https://github.com/mozilla/rust/issues/2374
13
// error-pattern:unsatisfied precondition constraint (for example, even(y
24

5+
36
fn print_even(y: int) : even(y) { log(debug, y); }
47

58
pure fn even(y: int) -> bool { true }
69

710
fn main() {
8-
let y: int = 42;
11+
let mut y = 42;
912
check (even(y));
1013
loop {
1114
print_even(y);
12-
do { do { do { y += 1; } while false } while false } while false
15+
loop { y += 1; break; }
1316
}
1417
}

src/test/compile-fail/do-while-constraints.rs renamed to src/test/compile-fail/while-constraints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ fn main() {
55
let x: int;
66
loop {
77
log(debug, y);
8-
do { do { do { x <- y; } while true } while true } while true
8+
while true { while true { while true { x <- y; } } }
99
}
1010
}

src/test/run-fail/do-while-body-fails.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/test/run-fail/do-while-fail.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// error-pattern:quux
2+
fn main() { let x: int = { while true { fail "quux"; } ; 8 } ; }

0 commit comments

Comments
 (0)