0
0
Rustprogramming~20 mins

Handling errors with match in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of match error handling with Result
What is the output of this Rust code when the input string is "42"?
Rust
fn parse_number(input: &str) -> Result<i32, String> {
    match input.parse::<i32>() {
        Ok(num) => Ok(num),
        Err(_) => Err(String::from("Failed to parse number")),
    }
}

fn main() {
    let result = parse_number("42");
    match result {
        Ok(n) => println!("Parsed number: {}", n),
        Err(e) => println!("Error: {}", e),
    }
}
ACompilation error
BError: Failed to parse number
CParsed number: 0
DParsed number: 42
Attempts:
2 left
💡 Hint
The input string "42" can be successfully converted to an integer.
Predict Output
intermediate
2:00remaining
Output when parsing fails in match
What is the output of this Rust code when the input string is "abc"?
Rust
fn parse_number(input: &str) -> Result<i32, String> {
    match input.parse::<i32>() {
        Ok(num) => Ok(num),
        Err(_) => Err(String::from("Failed to parse number")),
    }
}

fn main() {
    let result = parse_number("abc");
    match result {
        Ok(n) => println!("Parsed number: {}", n),
        Err(e) => println!("Error: {}", e),
    }
}
AError: Failed to parse number
BPanic at runtime
CCompilation error
DParsed number: 0
Attempts:
2 left
💡 Hint
The input string "abc" cannot be converted to an integer.
🔧 Debug
advanced
2:00remaining
Identify the error in match arms handling Result
What error does this Rust code produce when compiled?
Rust
fn main() {
    let result: Result<i32, &str> = Err("error");
    match result {
        Ok(num) => println!("Number: {}", num),
        Err(e) => println!("Error: {}", e),
        _ => println!("Unknown case"),
    }
}
ARuntime panic: no matching pattern
BCompilation error: unreachable pattern
CPrints: Error: error
DCompilation error: missing match arm
Attempts:
2 left
💡 Hint
The match arms cover all possible variants of Result already.
Predict Output
advanced
2:00remaining
Output of nested match error handling
What is the output of this Rust code?
Rust
fn divide(a: i32, b: i32) -> Result<i32, String> {
    if b == 0 {
        Err(String::from("Division by zero"))
    } else {
        Ok(a / b)
    }
}

fn main() {
    let result = divide(10, 0);
    match result {
        Ok(val) => println!("Result: {}", val),
        Err(e) => match e.as_str() {
            "Division by zero" => println!("Cannot divide by zero!"),
            _ => println!("Other error: {}", e),
        },
    }
}
ACannot divide by zero!
BOther error: Division by zero
CResult: 0
DCompilation error
Attempts:
2 left
💡 Hint
The error string matches "Division by zero" exactly.
🧠 Conceptual
expert
2:00remaining
Why use match for error handling in Rust?
Which statement best explains why Rust uses match expressions for handling errors with Result types?
ABecause match automatically retries failed operations without extra code.
BBecause match converts all errors into panics by default.
CBecause match forces exhaustive handling of all possible cases, preventing unhandled errors.
DBecause match allows ignoring errors silently without warnings.
Attempts:
2 left
💡 Hint
Think about safety and explicit handling in Rust.