The engine powering for loops under the hood is the Iterator trait. Let’s think about what we want to do. Each time we step through the for loop, we need to get the next item in a stream of values. We need some way to stop when there are no more items available. And for each Iterator, we need to know the type of the items. To handle all this, we’re going to use a trait called Iterator. Let’s look at a simplified version of it:

Get hands-on with 1000+ tech skills courses.