0
0
Rustprogramming~20 mins

Nested conditions in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Nested Conditions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested if-else blocks
What is the output of this Rust code snippet?
Rust
fn main() {
    let x = 7;
    if x > 5 {
        if x < 10 {
            println!("A");
        } else {
            println!("B");
        }
    } else {
        println!("C");
    }
}
AB
BA
CC
DNo output
Attempts:
2 left
๐Ÿ’ก Hint
Check the conditions step by step: is x greater than 5? Is it less than 10?
โ“ Predict Output
intermediate
2:00remaining
Nested match with conditions output
What will this Rust program print?
Rust
fn main() {
    let num = 3;
    match num {
        1..=5 => {
            if num % 2 == 0 {
                println!("Even");
            } else {
                println!("Odd");
            }
        }
        _ => println!("Out of range"),
    }
}
AOdd
BNo output
COut of range
DEven
Attempts:
2 left
๐Ÿ’ก Hint
Check if 3 is between 1 and 5, then check if it is even or odd.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in nested if conditions
This Rust code does not compile. What is the error?
Rust
fn main() {
    let x = 10;
    if x > 5 {
        if x < 15 {
            println!("Inside");
        }
    } else {
        println!("Outside");
    }
}
AVariable x is not mutable
BMissing braces for the outer if block
Cprintln! macro syntax error
DThe else is not matched with any if
Attempts:
2 left
๐Ÿ’ก Hint
Look at how Rust matches else with the nearest if.
โ“ Predict Output
advanced
2:00remaining
Output of nested if with else if
What does this Rust program print?
Rust
fn main() {
    let score = 85;
    if score >= 90 {
        println!("A");
    } else if score >= 80 {
        if score >= 85 {
            println!("B+");
        } else {
            println!("B");
        }
    } else {
        println!("C");
    }
}
AB
BC
CB+
DA
Attempts:
2 left
๐Ÿ’ก Hint
Check the score against each condition carefully.
๐Ÿง  Conceptual
expert
2:00remaining
How many times is the inner block executed?
Consider this Rust code snippet. How many times will the inner println! be executed?
Rust
fn main() {
    for i in 1..=5 {
        if i % 2 == 0 {
            if i > 2 {
                println!("{}", i);
            }
        }
    }
}
A1
B2
C3
D4
Attempts:
2 left
๐Ÿ’ก Hint
Check which numbers from 1 to 5 are even and greater than 2.