From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Nested loops

Nested loops

- [Narrator] In rust, multiple loops can be nested inside of one another. And one common reason for doing that is to process the elements of a multi-dimensional array. Let's say we want to display the elements of this three by three array named matrix. Since this is a two dimensional array we can use a pair of nested four loops to iterate through it and individually access each of the elements. Each iteration of the outer loop, iterating over the matrix or return a one-dimensional sub-array which we'll call a row. Then we'll use an inner loop to iterate through each row and return each of the elements, which will be numbers. Finally, within that inner loop, we'll print each number. Notice that I'm using print rather than print line, which means this print statement will not add a new line at the end of the string. Also notice I'm using this special sequence backslash T which we haven't encountered yet. This will insert a tab…

Contents