0
0
Rustprogramming~20 mins

Loop execution flow in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested loops with break
What is the output of this Rust code snippet?
Rust
fn main() {
    let mut count = 0;
    for i in 1..=3 {
        for j in 1..=3 {
            if i * j > 4 {
                break;
            }
            count += 1;
        }
    }
    println!("{}", count);
}
A9
B5
C7
D6
Attempts:
2 left
๐Ÿ’ก Hint
Count how many times the inner loop runs before break triggers.
โ“ Predict Output
intermediate
2:00remaining
Loop with continue and variable update
What will be printed by this Rust program?
Rust
fn main() {
    let mut sum = 0;
    for x in 1..=5 {
        if x % 2 == 0 {
            continue;
        }
        sum += x;
    }
    println!("{}", sum);
}
A6
B9
C15
D10
Attempts:
2 left
๐Ÿ’ก Hint
Sum only odd numbers from 1 to 5.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in this loop
What error will this Rust code produce when compiled?
Rust
fn main() {
    let mut i = 0;
    loop {
        if i == 5 {
            break;
        }
        i += 1;
    }
    println!("{}", i);
}
ANo error, prints 5
BRuntimeError: infinite loop
CSyntaxError: missing semicolon after i += 1
DSyntaxError: missing curly brace
Attempts:
2 left
๐Ÿ’ก Hint
Check if all statements end properly.
โ“ Predict Output
advanced
2:00remaining
Loop with labeled break
What will this Rust program print?
Rust
fn main() {
    let mut count = 0;
    'outer: for i in 1..=3 {
        for j in 1..=3 {
            if i + j == 4 {
                break 'outer;
            }
            count += 1;
        }
    }
    println!("{}", count);
}
A2
B6
C9
D3
Attempts:
2 left
๐Ÿ’ก Hint
The labeled break exits both loops when i+j == 4.
โ“ Predict Output
expert
2:00remaining
Complex loop with multiple control flow statements
What is the final value of `result` after running this Rust code?
Rust
fn main() {
    let mut result = 0;
    let mut i = 0;
    while i < 5 {
        i += 1;
        if i == 3 {
            continue;
        }
        if i == 4 {
            break;
        }
        result += i;
    }
    println!("{}", result);
}
A6
B5
C3
D4
Attempts:
2 left
๐Ÿ’ก Hint
Trace each iteration and note when continue and break happen.