Loops and decisions
Compute the running balance.
fn main() {
tx = [5000, -1200, 300]
mut total = 0
for amount in tx {
total = total + amount
}
println(total)
}
if
if total < 0 {
println(0)
} else {
println(total)
}
elif and else as you expect. Conditions are bool only.
while
mut i = 0
while i < 3 {
i = i + 1
}
Ranges
for i in 0..3 { } # 0, 1, 2
for i in 1..=3 { } # 1, 2, 3
break / continue
for amount in tx {
if amount == 0 { continue }
if total + amount < 0 { break }
total = total + amount
}
This is the loop in the integer_sum bench (Orbit fold, 0.00× C++ at n=1e8: same n, wrapping checksum). Write the obvious loop. Measure with --release before you clever it. See performance.
Next: functions.
