0
0
Rustprogramming~10 mins

Loop labels in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to label the outer loop correctly.

Rust
fn main() {
    'outer: for i in 1..=3 {
        for j in 1..=3 {
            if i == 2 && j == 2 {
                break [1];
            }
            println!("i = {}, j = {}", i, j);
        }
    }
}
Drag options to blanks, or click blank then click option'
A'label
B'inner
C'loop
D'outer
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a label that is not defined.
Omitting the apostrophe in the label name.
2fill in blank
medium

Complete the code to continue the outer loop when a condition is met.

Rust
fn main() {
    'outer: for i in 1..=3 {
        for j in 1..=3 {
            if j == 2 {
                continue [1];
            }
            println!("i = {}, j = {}", i, j);
        }
    }
}
Drag options to blanks, or click blank then click option'
A'inner
B'loop
C'outer
D'label
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using continue without a label inside nested loops.
Using a label that does not exist.
3fill in blank
hard

Fix the error in the code by choosing the correct label to break the outer loop.

Rust
fn main() {
    'start: for x in 1..=5 {
        for y in 1..=5 {
            if x * y > 6 {
                break [1];
            }
            println!("x = {}, y = {}", x, y);
        }
    }
}
Drag options to blanks, or click blank then click option'
A'start
B'outer
C'inner
D'loop
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a label that is not defined.
Using break without a label inside nested loops.
4fill in blank
hard

Fill both blanks to correctly label and break the outer loop.

Rust
fn main() {
    [1] for num in 1..=4 {
        for letter in ['a', 'b', 'c'].iter() {
            if num == 3 {
                break [2];
            }
            println!("{} - {}", num, letter);
        }
    }
}
Drag options to blanks, or click blank then click option'
A'outer:
B'inner:
C'outer
D'inner
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the label with colon in the break statement.
Mismatching label names between loop and break.
5fill in blank
hard

Fill all three blanks to label the loops and continue the outer loop correctly.

Rust
fn main() {
    [1] for i in 1..=3 {
        [2] for j in 1..=3 {
            if i == 2 && j == 1 {
                continue [3];
            }
            println!("i = {}, j = {}", i, j);
        }
    }
}
Drag options to blanks, or click blank then click option'
A'outer:
B'inner:
C'outer
D'inner
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the inner loop label in continue instead of the outer.
Including colon in the continue statement.