xxxxxxxxxx
//from https://doc.rust-lang.org/rust-by-example/flow_control/while.html
fn main() {
// A counter variable
let mut n = 1;
// Loop while `n` is less than 101
while n < 101 {
if n % 15 == 0 {
println!("fizzbuzz");
} else if n % 3 == 0 {
println!("fizz");
} else if n % 5 == 0 {
println!("buzz");
} else {
println!("{}", n);
}
// Increment counter
n += 1;
}
}
Rust has no do-while loop with syntax sugar. Use loop and break.
xxxxxxxxxx
loop {
doStuff();
if !c { break; }
}
xxxxxxxxxx
#![allow(unused)]
fn main() {
let mut i = 0;
while i < 10 {
println!("hello");
i = i + 1;
}
}
xxxxxxxxxx
// There are three types of loops in rust.
// 1. The `loop` loop
// Using `loop`, you can loop until you choose to break out of it.
// The following loops forever:
loop {}
// 2. The `while` loop
// Using `while`, you can loop while a condition is met.
// The following loops until x is less than 0:
while x >= 0 {}
// 3. The `for` loop
// Using `for`, you can loop over an iterator.
// Most collections (like [T] or Vec<T>) can be used with this type of loop.
// The following loops over every element in a vector:
for element in vector {}
xxxxxxxxxx
#![allow(unused)]
fn main() {
let mut i = 0;
while i < 10 {
println!("hello");
i = i + 1;
}
}
xxxxxxxxxx
// Rust provides a loop keyword to indicate an infinite loop.
// The break statement can be used to exit a loop at anytime, whereas the continue statement can be used to skip the rest of the iteration and start a new one.
loop {
// Your code that will run in an infinite loop here
}
https://doc.rust-lang.org/rust-by-example/flow_control/loop.html
xxxxxxxxxx
for variable in lower_bound_number..upper_bound_number {
// code block
}