Search⌘ K

Solution Review 3: Print a Right-Angled Triangle

Explore how to use nested for loops and the break statement in Rust to print a right-angled triangle. Understand loop iteration ranges and control flow to create patterns, preparing you for writing reusable functions next.

We'll cover the following...

Solution

Rust 1.40.0
fn test(n:i32) {
// define a nested for loop
for i in 0..n { //outer loop
for j in 0..i + 1 { // inner loop
print!("&");
}
println!("");
}
}
fn main(){
println!("Right angled triangle when n = 5 ");
test(5);
println!("Right angled triangle when n = 6 ");
test(6);
}

Explanation

The value n is given to you for which the right-angled triangle needs to be printed.

nested for loop

  • On line 3, in
...