Count to 10

We’ve been using while for looping for a while now. It’s taken a lot of self-restraint to not bring up for loops yet. However, to properly understand them, you need to know about traits, enums, and Option. Fortunately, now that we’ve covered these, we can finally use for loops.

Anything you can do with a for loop can also be done with a while loop. However, as you’ll see shortly, the for loop approach is usually much more pleasant to work with. You’re about to have a lot of fun simplifying some code.

Let’s write a program that’s going to count from 1 to 10 using a while loop. If you want to take a challenge exercise, implement it yourself before looking at the code below.

Get hands-on with 1200+ tech skills courses.