0
0
Rustprogramming~20 mins

Basic match usage in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using match?
Look at this Rust program. What will it print when run?
Rust
fn main() {
    let number = 3;
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        _ => println!("Other"),
    }
}
ATwo
BOther
COne
DThree
Attempts:
2 left
๐Ÿ’ก Hint
Check which pattern matches the value 3 in the match expression.
โ“ Predict Output
intermediate
2:00remaining
What does this Rust match expression return?
What value does this Rust code assign to 'result'?
Rust
fn main() {
    let x = 5;
    let result = match x {
        1..=4 => "small",
        5..=10 => "medium",
        _ => "large",
    };
    println!("{}", result);
}
Amedium
Bsmall
Clarge
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Look at the range patterns and see where 5 fits.
โ“ Predict Output
advanced
2:00remaining
What is the output of this nested match in Rust?
Analyze the nested match statements and find the output.
Rust
fn main() {
    let a = Some(2);
    let b = 3;
    match a {
        Some(x) => match b {
            3 => println!("x is {} and b is three", x),
            _ => println!("x is {} and b is not three", x),
        },
        None => println!("No value for a"),
    }
}
ANo value for a
Bx is 2 and b is three
Cx is 2 and b is not three
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Check the values inside the nested match carefully.
โ“ Predict Output
advanced
2:00remaining
What error does this Rust code produce?
What error will this Rust code cause when compiled?
Rust
fn main() {
    let num = 7;
    match num {
        1 => println!("One"),
        2 => println!("Two"),
        _ => (),
    }
}
AError: variable 'num' not found
BError: mismatched types
CError: non-exhaustive patterns in match
DNo error, prints nothing
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens if 'num' is 7 and no match arm covers it.
๐Ÿง  Conceptual
expert
2:00remaining
How many arms does this Rust match expression have?
Count the number of arms in this Rust match expression.
Rust
fn main() {
    let val = 10;
    match val {
        1 | 2 | 3 => println!("One, two or three"),
        4..=10 => println!("Between four and ten"),
        _ => println!("Something else"),
    }
}
A3
B2
C4
D5
Attempts:
2 left
๐Ÿ’ก Hint
Count each separate match arm, not the number of values in patterns.