For Loops
Rust has for loops which allow us to run the same
block of code multiple times.
The formula for a for loop that runs through a range
of numbers is:
- The
forkeyword - A variable name to hold the value of the number on each loop
- The
inkeyword - The range of numbers
- The code block to run each time though the loop
The range has the format of:
- The starting number
- Two dots followed by an equal sign (i.e.
..=) - The ending number
For example:
1..=5
And here's a full example that outputs:
alfa is 1
alfa is 2
alfa is 3
alfa is 4
alfa is 5
SOURCE CODE
fn main() {
for alfa in 1..=5 {
println!("alfa is {alfa}");
}
}
CODE RUNNER
Typos: 0