Challenge - 5 Problems
Rust Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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), } }
Attempts:
2 left
💡 Hint
The input string "42" can be successfully converted to an integer.
✗ Incorrect
The input "42" parses correctly to the integer 42, so the Ok branch is taken and prints "Parsed number: 42".
❓ Predict Output
intermediate2: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), } }
Attempts:
2 left
💡 Hint
The input string "abc" cannot be converted to an integer.
✗ Incorrect
Parsing "abc" to i32 fails, so the Err branch is taken and prints the error message.
🔧 Debug
advanced2: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"),
}
}Attempts:
2 left
💡 Hint
The match arms cover all possible variants of Result already.
✗ Incorrect
The wildcard arm '_' is unreachable because Result only has Ok and Err variants, causing a compilation error.
❓ Predict Output
advanced2: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), }, } }
Attempts:
2 left
💡 Hint
The error string matches "Division by zero" exactly.
✗ Incorrect
The error string matches the first pattern in the nested match, so it prints the custom message.
🧠 Conceptual
expert2:00remaining
Why use match for error handling in Rust?
Which statement best explains why Rust uses match expressions for handling errors with Result types?
Attempts:
2 left
💡 Hint
Think about safety and explicit handling in Rust.
✗ Incorrect
Rust's match requires handling all variants of Result, ensuring no error case is missed, which improves safety.