也有英文版文章
Also this tutorial has been written in English
Check out my Medium
fn main() {
loop {
println!("再一次!");
}
}
$ cargo run
Compiling loops v0.1.0 (file:///projects/loops)
Finished dev [unoptimized + debuginfo] target(s) in 0.29s
Running `target/debug/loops`
再一次!
再一次!
再一次!
再一次!
^C再一次!
fn main() {
let mut n = 1;
loop {
println!("{:?}", n);
if n == 4 {
break;
}
n = n + 1;
}
}
/*
Output
1
2
3
4
*/
fn main() {
let mut counter = 5;
while counter >= 1 {
println!("{:?}", counter);
counter = counter - 1;
}
println!("done!");
}
/*
Output
5
4
3
2
1
done!
*/
或者換個寫法
fn main() {
let mut counter = 5;
let mut done = false;
while !done {
if counter > 0 {
println!("{:?}", counter);
counter = counter - 1;
} else {
done = true;
}
}
println!("done!");
}