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

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.

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

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 } ; }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// error-pattern:giraffe
2+
fn main() {
3+
fail { while true { fail "giraffe"}; "clandestine" };
4+
}

src/test/run-pass/break.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ fn main() {
44
let mut i = 0;
55
while i < 20 { i += 1; if i == 10 { break; } }
66
assert (i == 10);
7-
do { i += 1; if i == 20 { break; } } while i < 30
7+
loop { i += 1; if i == 20 { break; } }
88
assert (i == 20);
99
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
1010
if x == 3 { break; } assert (x <= 3);
1111
}
1212
i = 0;
1313
while i < 10 { i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0); }
1414
i = 0;
15-
do { i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0); } while i < 10
15+
loop {
16+
i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0);
17+
if i >= 10 { break; }
18+
}
1619
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
1720
if x % 2 == 0 { cont; }
1821
assert (x % 2 != 0);

src/test/run-pass/issue-1257.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fn main () {
22
let mut line = "";
33
let mut i = 0;
4-
do {
4+
while line != "exit" {
55
line = if i == 9 { "exit" } else { "notexit" };
66
i += 1;
7-
} while line != "exit";
7+
}
88
}

src/test/run-pass/weird-exprs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn what() {
1616
}
1717

1818
fn zombiejesus() {
19-
do {
19+
loop {
2020
while (ret) {
2121
if (ret) {
2222
alt (ret) {
@@ -33,7 +33,8 @@ fn zombiejesus() {
3333
ret;
3434
}
3535
}
36-
} while ret
36+
if (ret) { break; }
37+
}
3738
}
3839

3940
fn notsure() {
@@ -58,7 +59,7 @@ fn canttouchthis() -> uint {
5859
fn angrydome() {
5960
loop { if break { } }
6061
let mut i = 0;
61-
do { i += 1; if i == 1 { alt check cont { 1 { } } } } while false
62+
loop { i += 1; if i == 1 { alt check cont { 1 { } } } break; }
6263
}
6364

6465
fn evil_lincoln() { let evil <- #debug("lincoln"); }

src/test/run-pass/while-and-do-while.rs renamed to src/test/run-pass/while.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ fn main() {
44
let mut x: int = 10;
55
let mut y: int = 0;
66
while y < x { log(debug, y); #debug("hello"); y = y + 1; }
7-
do {
7+
while x > 0 {
88
#debug("goodbye");
99
x = x - 1;
1010
log(debug, x);
11-
} while x > 0
11+
}
1212
}

0 commit comments

Comments
 (0)