Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let mut s = 0;
for i in 1..4 {
s += i;
}
println!("{s}");
}6
- Expecting
10: the range1..4excludes its end, soitakes 1, 2 and 3; the form that reaches 4 is the inclusive1..=4. - Expecting the range to skip its start:
a..bbegins ataitself and stops just beforeb.
- How would you change the range so that the loop adds 1 through 4?
- What does
for i in 4..1print, and how do you count down instead?