Rust Error Handling Quiz
Master Rust's robust error handling system with Result, Option, and the ? operator for reliable code
Question 1
What is the Result type in Rust?
Question 2
What does this Result example show?
fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err(String::from("Division by zero"))
} else {
Ok(a / b)
}
}Question 3
What is the Option type?
Question 4
What does this Option example show?
fn find_first_even(nums: &[i32]) -> Option<i32> {
for &num in nums {
if num % 2 == 0 {
return Some(num);
}
}
None
}Question 5
What is the ? operator used for?
Question 6
What does this ? operator example do?
fn read_file() -> Result<String, std::io::Error> {
let content = std::fs::read_to_string("file.txt")?;
Ok(content)
}Question 7
What is the difference between unwrap() and expect()?
Question 8
When should you use unwrap() or expect()?
Question 9
What does the map() method do on Result?
Question 10
What does this Result map() example do?
let result: Result<i32, &str> = Ok(5);
let doubled = result.map(|x| x * 2);Question 11
What does map_err() do?
Question 12
What does this map_err() example do?
let result: Result<i32, &str> = Err("error");
let mapped = result.map_err(|e| format!("Error: {}", e));Question 13
What does and_then() do?
Question 14
What does this and_then() example do?
let result = Ok(5).and_then(|x| Ok(x * 2));Question 15
What does or_else() do?
Question 16
What does unwrap_or() do for Option?
Question 17
What does this unwrap_or() example return?
let option: Option<i32> = None;
let value = option.unwrap_or(42);Question 18
What does unwrap_or_else() do?
Question 19
What is a custom error enum?
Question 20
What does this custom error enum example show?
#[derive(Debug)]
enum MyError {
Io(std::io::Error),
Parse(std::num::ParseIntError),
Custom(String),
}Question 21
How do you implement std::error::Error for custom errors?
Question 22
What is the From trait used for in error handling?
Question 23
What does this From implementation enable?
impl From<std::io::Error> for MyError {
fn from(error: std::io::Error) -> Self {
MyError::Io(error)
}
}Question 24
What is the Box<dyn std::error::Error> pattern?
Question 25
What does this Box<dyn Error> example show?
fn dynamic_error() -> Result<(), Box<dyn std::error::Error>> {
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "error")))
}Question 26
What is error propagation?
Question 27
What does this error propagation example show?
fn outer() -> Result<(), std::io::Error> {
inner()?;
Ok(())
}
fn inner() -> Result<(), std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::Other, "error"))
}Question 28
What is the difference between panic! and returning Result?
Question 29
What is the Result<T, E> syntax sugar?
Question 30
What does Ok(()) represent?
Question 31
What is the try! macro?
Question 32
What does this pattern matching on Result show?
match divide(10, 2) {
Ok(result) => println!("Result: {}", result),
Err(error) => println!("Error: {}", error),
}Question 33
What does if let work with Results?
if let Ok(value) = divide(10, 2) {
println!("Success: {}", value);
}Question 34
What is error chaining?
Question 35
What does this error chaining example show?
fn complex_operation() -> Result<i32, std::io::Error> {
let file = std::fs::File::open("data.txt")?;
let content = read_content(file)?;
let result = parse_number(content)?;
Ok(result)
}Question 36
What is the main advantage of Result over exceptions?
Question 37
What is the main advantage of Option over null?
Question 38
What does expect() do differently from unwrap()?
let value = Some(5).expect("Value should exist");Question 39
What is error downcasting?
Question 40
In a complex application where you have multiple layers (database, business logic, API), how should you handle errors to maintain good separation of concerns while providing meaningful error messages to users?
