Remove do ... while
loops from the tests and docs.
This commit is contained in:
parent
13c924c049
commit
f943667af3
15 changed files with 45 additions and 79 deletions
33
doc/rust.md
33
doc/rust.md
|
@ -1991,28 +1991,19 @@ way.
|
||||||
|
|
||||||
*TODO*.
|
*TODO*.
|
||||||
|
|
||||||
### While expressions
|
### While loops
|
||||||
|
|
||||||
~~~~~~~~{.ebnf .gram}
|
~~~~~~~~{.ebnf .gram}
|
||||||
while_expr : "while" expr '{' block '}'
|
while_expr : "while" expr '{' block '}'
|
||||||
| "do" '{' block '}' "while" expr ;
|
| "do" '{' block '}' "while" expr ;
|
||||||
~~~~~~~~
|
~~~~~~~~
|
||||||
|
|
||||||
A `while` expression is a loop construct. A `while` loop may be either a
|
A `while` loop begins by evaluating the boolean loop conditional expression.
|
||||||
simple `while` or a `do`-`while` loop.
|
If the loop conditional expression evaluates to `true`, the loop body block
|
||||||
|
executes and control returns to the loop conditional expression. If the loop
|
||||||
|
conditional expression evaluates to `false`, the `while` expression completes.
|
||||||
|
|
||||||
In the case of a simple `while`, the loop begins by evaluating the boolean
|
An example:
|
||||||
loop conditional expression. If the loop conditional expression evaluates to
|
|
||||||
`true`, the loop body block executes and control returns to the loop
|
|
||||||
conditional expression. If the loop conditional expression evaluates to
|
|
||||||
`false`, the `while` expression completes.
|
|
||||||
|
|
||||||
In the case of a `do`-`while`, the loop begins with an execution of the loop
|
|
||||||
body. After the loop body executes, it evaluates the loop conditional
|
|
||||||
expression. If it evaluates to `true`, control returns to the beginning of the
|
|
||||||
loop body. If it evaluates to `false`, control exits the loop.
|
|
||||||
|
|
||||||
An example of a simple `while` expression:
|
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
# let mut i = 0;
|
# let mut i = 0;
|
||||||
|
@ -2024,18 +2015,6 @@ while i < 10 {
|
||||||
}
|
}
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
An example of a `do`-`while` expression:
|
|
||||||
|
|
||||||
~~~~
|
|
||||||
# let mut i = 0;
|
|
||||||
# let println = io::println;
|
|
||||||
|
|
||||||
do {
|
|
||||||
println("hello\n");
|
|
||||||
i = i + 1;
|
|
||||||
} while i < 10;
|
|
||||||
~~~~
|
|
||||||
|
|
||||||
### Infinite loops
|
### Infinite loops
|
||||||
|
|
||||||
A `loop` expression denotes an infinite loop:
|
A `loop` expression denotes an infinite loop:
|
||||||
|
|
|
@ -667,6 +667,15 @@ a specific value, are not allowed.
|
||||||
keyword `break` can be used to abort the loop, and `cont` can be used
|
keyword `break` can be used to abort the loop, and `cont` can be used
|
||||||
to abort the current iteration and continue with the next.
|
to abort the current iteration and continue with the next.
|
||||||
|
|
||||||
|
~~~~
|
||||||
|
let mut cake_amount = 8;
|
||||||
|
while cake_amount > 0 {
|
||||||
|
cake_amount -= 1;
|
||||||
|
}
|
||||||
|
~~~~
|
||||||
|
|
||||||
|
`loop` is the preferred way of writing `while true`:
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
let mut x = 5;
|
let mut x = 5;
|
||||||
while true {
|
while true {
|
||||||
|
@ -679,17 +688,6 @@ while true {
|
||||||
This code prints out a weird sequence of numbers and stops as soon as
|
This code prints out a weird sequence of numbers and stops as soon as
|
||||||
it finds one that can be divided by five.
|
it finds one that can be divided by five.
|
||||||
|
|
||||||
There's also `while`'s ugly cousin, `do`/`while`, which does not check
|
|
||||||
its condition on the first iteration, using traditional syntax:
|
|
||||||
|
|
||||||
~~~~
|
|
||||||
# fn eat_cake() {}
|
|
||||||
# fn any_cake_left() -> bool { false }
|
|
||||||
do {
|
|
||||||
eat_cake();
|
|
||||||
} while any_cake_left();
|
|
||||||
~~~~
|
|
||||||
|
|
||||||
For more involved iteration, such as going over the elements of a
|
For more involved iteration, such as going over the elements of a
|
||||||
collection, Rust uses higher-order functions. We'll come back to those
|
collection, Rust uses higher-order functions. We'll come back to those
|
||||||
in a moment.
|
in a moment.
|
||||||
|
@ -2496,12 +2494,12 @@ Here is the function which implements the child task:
|
||||||
fn stringifier(from_parent: comm::port<uint>,
|
fn stringifier(from_parent: comm::port<uint>,
|
||||||
to_parent: comm::chan<str>) {
|
to_parent: comm::chan<str>) {
|
||||||
let mut value: uint;
|
let mut value: uint;
|
||||||
do {
|
loop {
|
||||||
value = comm::recv(from_parent);
|
value = comm::recv(from_parent);
|
||||||
comm::send(to_parent, uint::to_str(value, 10u));
|
comm::send(to_parent, uint::to_str(value, 10u));
|
||||||
} while value != 0u;
|
if value == 0u { break; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~~~~
|
~~~~
|
||||||
|
|
||||||
You can see that the function takes two parameters. The first is a
|
You can see that the function takes two parameters. The first is a
|
||||||
|
|
|
@ -61,24 +61,6 @@ fn while_aliased_mut_cond(cond: bool, cond2: bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_while_aliased_mut(cond: bool) {
|
|
||||||
let mut v = ~3, w = ~4;
|
|
||||||
let mut _x = &mut w;
|
|
||||||
do {
|
|
||||||
borrow(v); //! ERROR loan of mutable local variable as immutable conflicts with prior loan
|
|
||||||
_x = &mut v; //! NOTE prior loan as mutable granted here
|
|
||||||
} while cond;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn loop_in_block() {
|
|
||||||
let mut v = ~3, w = ~4;
|
|
||||||
let mut _x = &mut w;
|
|
||||||
uint::range(0u, 10u) {|_i|
|
|
||||||
borrow(v); //! ERROR loan of mutable local variable as immutable conflicts with prior loan
|
|
||||||
_x = &mut v; //! NOTE prior loan as mutable granted here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn at_most_once_block() {
|
fn at_most_once_block() {
|
||||||
fn at_most_once(f: fn()) { f() }
|
fn at_most_once(f: fn()) { f() }
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ fn foo() -> int {
|
||||||
let x: int;
|
let x: int;
|
||||||
let i: int;
|
let i: int;
|
||||||
|
|
||||||
do { i = 0; break; x = 0; } while x != 0
|
loop { i = 0; break; x = 0; }
|
||||||
|
|
||||||
log(debug, x);
|
log(debug, x);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ fn foo() -> int {
|
||||||
let x: int;
|
let x: int;
|
||||||
let i: int;
|
let i: int;
|
||||||
|
|
||||||
do { i = 0; break; x = 0; } while 1 != 2
|
while 1 != 2 { i = 0; break; x = 0; }
|
||||||
|
|
||||||
log(debug, x);
|
log(debug, x);
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
|
// xfail-test
|
||||||
|
// https://github.com/mozilla/rust/issues/2374
|
||||||
// error-pattern:unsatisfied precondition constraint (for example, even(y
|
// error-pattern:unsatisfied precondition constraint (for example, even(y
|
||||||
|
|
||||||
|
|
||||||
fn print_even(y: int) : even(y) { log(debug, y); }
|
fn print_even(y: int) : even(y) { log(debug, y); }
|
||||||
|
|
||||||
pure fn even(y: int) -> bool { true }
|
pure fn even(y: int) -> bool { true }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let y: int = 42;
|
let mut y = 42;
|
||||||
check (even(y));
|
check (even(y));
|
||||||
loop {
|
loop {
|
||||||
print_even(y);
|
print_even(y);
|
||||||
do { do { do { y += 1; } while false } while false } while false
|
loop { y += 1; break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,6 @@ fn main() {
|
||||||
let x: int;
|
let x: int;
|
||||||
loop {
|
loop {
|
||||||
log(debug, y);
|
log(debug, y);
|
||||||
do { do { do { x <- y; } while true } while true } while true
|
while true { while true { while true { x <- y; } } }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,2 +0,0 @@
|
||||||
// error-pattern:quux
|
|
||||||
fn main() { let x: int = do { fail "quux"; } while true; }
|
|
|
@ -1,4 +0,0 @@
|
||||||
// error-pattern:giraffe
|
|
||||||
fn main() {
|
|
||||||
fail do { fail "giraffe" } while true;
|
|
||||||
}
|
|
2
src/test/run-fail/while-body-fails.rs
Normal file
2
src/test/run-fail/while-body-fails.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
// error-pattern:quux
|
||||||
|
fn main() { let x: int = { while true { fail "quux"; } ; 8 } ; }
|
4
src/test/run-fail/while-fail.rs
Normal file
4
src/test/run-fail/while-fail.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// error-pattern:giraffe
|
||||||
|
fn main() {
|
||||||
|
fail { while true { fail "giraffe"}; "clandestine" };
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ fn main() {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < 20 { i += 1; if i == 10 { break; } }
|
while i < 20 { i += 1; if i == 10 { break; } }
|
||||||
assert (i == 10);
|
assert (i == 10);
|
||||||
do { i += 1; if i == 20 { break; } } while i < 30
|
loop { i += 1; if i == 20 { break; } }
|
||||||
assert (i == 20);
|
assert (i == 20);
|
||||||
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
|
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
|
||||||
if x == 3 { break; } assert (x <= 3);
|
if x == 3 { break; } assert (x <= 3);
|
||||||
|
@ -12,7 +12,10 @@ fn main() {
|
||||||
i = 0;
|
i = 0;
|
||||||
while i < 10 { i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0); }
|
while i < 10 { i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0); }
|
||||||
i = 0;
|
i = 0;
|
||||||
do { i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0); } while i < 10
|
loop {
|
||||||
|
i += 1; if i % 2 == 0 { cont; } assert (i % 2 != 0);
|
||||||
|
if i >= 10 { break; }
|
||||||
|
}
|
||||||
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
|
for vec::each([1, 2, 3, 4, 5, 6]) {|x|
|
||||||
if x % 2 == 0 { cont; }
|
if x % 2 == 0 { cont; }
|
||||||
assert (x % 2 != 0);
|
assert (x % 2 != 0);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
fn main () {
|
fn main () {
|
||||||
let mut line = "";
|
let mut line = "";
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
do {
|
while line != "exit" {
|
||||||
line = if i == 9 { "exit" } else { "notexit" };
|
line = if i == 9 { "exit" } else { "notexit" };
|
||||||
i += 1;
|
i += 1;
|
||||||
} while line != "exit";
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ fn what() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zombiejesus() {
|
fn zombiejesus() {
|
||||||
do {
|
loop {
|
||||||
while (ret) {
|
while (ret) {
|
||||||
if (ret) {
|
if (ret) {
|
||||||
alt (ret) {
|
alt (ret) {
|
||||||
|
@ -33,7 +33,8 @@ fn zombiejesus() {
|
||||||
ret;
|
ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ret
|
if (ret) { break; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn notsure() {
|
fn notsure() {
|
||||||
|
@ -58,7 +59,7 @@ fn canttouchthis() -> uint {
|
||||||
fn angrydome() {
|
fn angrydome() {
|
||||||
loop { if break { } }
|
loop { if break { } }
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
do { i += 1; if i == 1 { alt check cont { 1 { } } } } while false
|
loop { i += 1; if i == 1 { alt check cont { 1 { } } } break; }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evil_lincoln() { let evil <- #debug("lincoln"); }
|
fn evil_lincoln() { let evil <- #debug("lincoln"); }
|
||||||
|
|
|
@ -4,9 +4,9 @@ fn main() {
|
||||||
let mut x: int = 10;
|
let mut x: int = 10;
|
||||||
let mut y: int = 0;
|
let mut y: int = 0;
|
||||||
while y < x { log(debug, y); #debug("hello"); y = y + 1; }
|
while y < x { log(debug, y); #debug("hello"); y = y + 1; }
|
||||||
do {
|
while x > 0 {
|
||||||
#debug("goodbye");
|
#debug("goodbye");
|
||||||
x = x - 1;
|
x = x - 1;
|
||||||
log(debug, x);
|
log(debug, x);
|
||||||
} while x > 0
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue