rust loop code example
Example 1: for loops in rust
for x in 0..10 {
println!("{}", x);
}
for var in expression {
code
}
Example 2: rust while loop
fn main() {
let mut n = 1;
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);
}
n += 1;
}
}
Example 3: loop in rust
#![allow(unused_variables)]
fn main() {
let mut x = 5;
let mut done = false;
while !done {
x += x - 3;
println!("{}", x);
if x % 5 == 0 {
done = true;
}
}
}