Greetings! In this lesson, we're going to delve into another essential concept in Rust programming: Nested Loops. You've learned the fundamentals of looping structures in previous lessons, including the usage of for
, while
, and loop
. Now, we're going to layer these loops to handle more complex scenarios and data manipulations.
Nested loops allow us to perform iterations within iterations. Think of it as another level of repetition where one loop runs inside another loop. This capability is vital for tasks such as multidimensional array processing, creating patterns, and more complex data manipulations.
Let's get started!
Nested for
loops are a powerful tool for iterating over multidimensional arrays or creating patterns. Here’s a basic example to get you started:
Rust1fn main() { 2 // Start of outer loop 3 for i in 1..4 { 4 println!("Outer loop iteration {}", i); 5 // Start of inner loop 6 for j in 1..4 { 7 println!("i: {}, j: {}", i, j); 8 } 9 } 10} 11/* Output 12 Outer loop iteration 1 13 i: 1, j: 1 14 i: 1, j: 2 15 i: 1, j: 3 16 Outer loop iteration 2 17 i: 2, j: 1 18 i: 2, j: 2 19 i: 2, j: 3 20 Outer loop iteration 3 21 i: 3, j: 1 22 i: 3, j: 2 23 i: 3, j: 3 24*/
In this example:
- The outer loop runs with
i
ranging from 1 to 3. - The inner loop runs with
j
ranging from 1 to 3 for each iteration of the outer loop.
Nested while
loops offer similar functionality.
Rust1fn main() { 2 let mut i = 1; 3 // Start of outer loop 4 while i < 4 { 5 println!("Outer loop iteration {}", i); 6 let mut j = 1; 7 // Start of inner loop 8 while j < 4 { 9 println!("i: {}, j: {}", i, j); 10 j += 1; 11 } 12 i += 1; 13 } 14} 15/* Output 16 Outer loop iteration 1 17 i: 1, j: 1 18 i: 1, j: 2 19 i: 1, j: 3 20 Outer loop iteration 2 21 i: 2, j: 1 22 i: 2, j: 2 23 i: 2, j: 3 24 Outer loop iteration 3 25 i: 3, j: 1 26 i: 3, j: 2 27 i: 3, j: 3 28*/
In this example:
- We initialized
i
andj
outside the loops. - The outer
while
loop iterates whilei
is less than 4. - Inside the outer loop, we have another
while
loop iterating whilej
is less than 4, printing the values ofi
andj
.
For more advanced funcationality, the inner loop can use a range dependant on the outer loop variable. The syntax 0..=i
creates a range from 0 up to and including i
.
Rust1fn main() { 2 for i in 0..5 { 3 // Inner loop iterates from 0 up to and including i 4 for j in 0..=i { 5 print!("{} ", j); 6 } 7 println!(); 8 } 9} 10/* Output 11 0 12 0 1 13 0 1 2 14 0 1 2 3 15 0 1 2 3 4 16*/
In this example:
- The outer loop runs with
i
ranging from 0 to 4. - For each iteration of the outer loop, the inner loop runs with
j
ranging from 0 toi
.
Similar to nested for
loops, nested while
loops can also create advanced patterns or manipulate data:
Rust1fn main() { 2 let mut i = 5; 3 while i > 0 { 4 let mut j = i; 5 while j > 0 { 6 print!("{} ", j); 7 j -= 1; 8 } 9 println!(); 10 i -= 1; 11 } 12} 13/* Output 14 5 4 3 2 1 15 4 3 2 1 16 3 2 1 17 2 1 18 1 19*/
In this example:
- The outer loop iterates with
i
decrementing from 5 to 1. - The inner loop starts with
j
taking on the value ofi
, decrementingj
until it reaches 0.
You can mix for
loops with while
loops to leverage the strengths of both constructs:
Rust1fn main() { 2 for i in 1..4 { 3 let mut j = i; 4 while j > 0 { 5 println!("i: {}, j: {}", i, j); 6 j -= 1; 7 } 8 } 9} 10/* Output 11 i: 1, j: 1 12 i: 2, j: 2 13 i: 2, j: 1 14 i: 3, j: 3 15 i: 3, j: 2 16 i: 3, j: 1 17*/
In this example:
- The outer
for
loop runs withi
ranging from 1 to 3. - The inner
while
loop starts withj
taking on the value ofi
, decrementingj
until it reaches 0.
Great job! You've now explored the use of nested loops in Rust, including basic and advanced examples for both for
and while
loops. You've also seen how to combine these loops for greater control and flexibility in your programs.
Understanding nested loops expands your ability to handle complex data structures and create intricate algorithms. It's a powerful skill that will be highly beneficial in advanced programming tasks, such as multidimensional array manipulations and complex pattern creations.
Now, let's move on to the practice exercises where you can apply what you've learned. These exercises will help you solidify your understanding and get comfortable with using nested loops in various scenarios. Enjoy coding!