Loops
Overview of while and for.
Sum with for
while repeats while the condition is true. for i in a..b iterates a half-open range.
fn main() {
let mut sum = 0
for i in 0..5 {
sum = sum + i
}
print(sum)
}fn main() -> void {
let mut sum: i32 = 0
for i in 0..5 {
sum = sum + i
}
print(sum)
}Output
10