Challenge - 5 Problems
Result Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Rust code using Result?
Consider this Rust code snippet using the
Result enum. What will it print?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() { match divide(10, 2) { Ok(val) => println!("Result is {}", val), Err(e) => println!("Error: {}", e), } }
Attempts:
2 left
💡 Hint
Check what happens when dividing 10 by 2 and how the match handles Ok and Err.
✗ Incorrect
The function divides 10 by 2, which is valid, so it returns Ok(5). The match prints the value inside Ok, so it prints 'Result is 5'.
❓ Predict Output
intermediate2:00remaining
What error message does this code produce?
Look at this Rust code using
Result. What error message will it print?Rust
fn parse_number(s: &str) -> Result<i32, String> { s.parse::<i32>().map_err(|_| String::from("parse error")) } fn main() { match parse_number("abc") { Ok(n) => println!("Number: {}", n), Err(e) => println!("Error: {}", e), } }
Attempts:
2 left
💡 Hint
The string "abc" cannot be parsed as a number, so the error branch runs.
✗ Incorrect
The parse fails and map_err converts the parse error to the string "parse error". So the error message printed is 'Error: parse error'.
🔧 Debug
advanced2:00remaining
Why does this Rust code cause a compilation error?
This Rust code tries to unwrap a Result but causes a compilation error. Why?
Rust
fn get_value(flag: bool) -> Result<i32, String> { if flag { Ok(42) } else { Err("no value".to_string()) } } fn main() { let val = get_value(true).unwrap_or("default"); println!("Value: {}", val); }
Attempts:
2 left
💡 Hint
Check the type expected by unwrap_or and the type of the argument passed.
✗ Incorrect
unwrap_or expects a value of the Ok type (i32), but "default" is a string, so the compiler reports a type mismatch error.
❓ Predict Output
advanced2:00remaining
What is the output of this Rust code using ? operator with Result?
Analyze this Rust code that uses the
? operator with Result. What will it print?Rust
fn try_divide(a: i32, b: i32) -> Result<i32, String> { if b == 0 { Err("division by zero".to_string()) } else { Ok(a / b) } } fn main() { let result = try_divide(10, 0).unwrap_or_else(|e| { println!("Error handled: {}", e); -1 }); println!("Result: {}", result); }
Attempts:
2 left
💡 Hint
The ? operator is not used here, but unwrap_or_else handles the error case.
✗ Incorrect
try_divide returns Err for division by zero. unwrap_or_else runs the closure printing the error and returns -1. Then main prints 'Result: -1'.
🧠 Conceptual
expert2:00remaining
How many times is the closure executed in this Rust code using Result and map_err?
Consider this Rust code snippet. How many times will the closure inside
map_err be executed?Rust
fn main() {
let res: Result<i32, &str> = Err("fail");
let new_res = res.map_err(|e| {
println!("Handling error: {}", e);
"handled"
});
let final_res = new_res.map_err(|e| {
println!("Second handler: {}", e);
"final"
});
}Attempts:
2 left
💡 Hint
Each map_err runs only if the Result is Err. The Result stays Err after the first map_err.
✗ Incorrect
The original Result is Err, so the first map_err closure runs once, printing 'Handling error: fail'. It returns a new Err, so the second map_err closure also runs once, printing 'Second handler: handled'. Total 2 times.