Search⌘ K

Counting

Explore counting techniques in Rust using mutable variables and while loops. Understand common off-by-one errors and how to correct them. This lesson helps you predict loop outputs and prepares you to transition to more efficient loops.

We'll cover the following...

One of the most common bugs in computer programming is an off-by-one error. Oftentimes, we end up writing programs that do either one too many, or one too few times. Let’s see if you can predict the output of this program:

Rust 1.40.0
fn main() {
let mut i = 1;
while i < 10 {
println!("i == {}", i);
i += 1;
}
}

It kind ...