0
0
Rustprogramming~20 mins

Loop labels in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Loop Labels Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested loops with labels
What is the output of this Rust code using loop labels?
Rust
fn main() {
    let mut count = 0;
    'outer: loop {
        println!("Outer loop start");
        'inner: loop {
            count += 1;
            if count == 2 {
                break 'outer;
            }
            break 'inner;
        }
        println!("Outer loop end");
    }
    println!("Count: {}", count);
}
A
Outer loop start
Count: 2
B
Outer loop start
Outer loop end
Outer loop start
Count: 2
C
Outer loop start
Outer loop end
Count: 2
D
Outer loop start
Outer loop end
Outer loop start
Outer loop end
Count: 2
Attempts:
2 left
๐Ÿ’ก Hint
Look at where the break statements with labels exit the loops.
โ“ Predict Output
intermediate
2:00remaining
Value of variable after labeled loops
What is the value of variable `i` after running this Rust code?
Rust
fn main() {
    let mut i = 0;
    'counting_up: loop {
        i += 1;
        'counting_down: loop {
            i -= 1;
            break 'counting_up;
        }
    }
    println!("i = {}", i);
}
Ai = 0
Bi = 1
Ci = -1
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Check which loop the break statement exits and when increments/decrements happen.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error with loop labels
What error does this Rust code produce?
Rust
fn main() {
    let mut x = 0;
    'outer: loop {
        'inner: loop {
            if x > 5 {
                break 'outer;
            }
            x += 1;
            break 'inner;
        }
        break 'inner;
    }
    println!("x = {}", x);
}
Ax = 6
Berror: unused label 'inner
Cx = 1
Derror: cannot break to label 'inner from outside its loop
Attempts:
2 left
๐Ÿ’ก Hint
Check where the break statements with labels are used relative to their loops.
๐Ÿ“ Syntax
advanced
2:00remaining
Which option correctly uses loop labels to break outer loop?
Which code snippet correctly breaks the outer loop from inside the inner loop using labels?
A
fn main() {
    'outer: loop {
        loop {
            break 'outer;
        }
    }
}
B
fn main() {
    loop 'outer {
        loop {
            break 'outer;
        }
    }
}
C
fn main() {
    'outer: loop {
        loop {
            break outer;
        }
    }
}
D
fn main() {
    'outer: loop {
        loop {
            break 'inner;
        }
    }
}
Attempts:
2 left
๐Ÿ’ก Hint
Remember the syntax for labeling loops and breaking to labels.
๐Ÿš€ Application
expert
2:00remaining
How many times does the inner loop run?
Consider this Rust code. How many times does the inner loop execute before the program ends?
Rust
fn main() {
    let mut count = 0;
    'outer: loop {
        'inner: loop {
            count += 1;
            if count == 3 {
                break 'outer;
            }
            break 'inner;
        }
    }
    println!("Count: {}", count);
}
A1
B2
C3
DInfinite loop
Attempts:
2 left
๐Ÿ’ก Hint
Count how many times the inner loop increments before breaking the outer loop.