Repeat with a while loop
A while loop repeats a step while its condition is true.
Simple example
Start1 → 3
Result1
2
3
fn main() {
let mut number: i64 = 1;
while number <= 3 {
println!("{}", number);
number += 1;
}
}
1 2 3
Advertisements
Everyday examples
A ticket roll starts at 21. Show the next three ticket numbers, starting with 21.
Start21 → 23
Result21
22
23
fn main() {
let mut ticket: i64 = 21;
while ticket <= 23 {
println!("{}", ticket);
ticket += 1;
}
}
21 22 23