What does the match statement do when handling errors in Rust?
It checks the result of an operation and lets you handle different cases, like Ok for success and Err for errors, by matching on these variants.
Click to reveal answer
beginner
How do you access the value inside an Ok variant using match?
You use a pattern like Ok(value) in the match arm to get the successful result stored in value.
Click to reveal answer
beginner
What is the purpose of the Err(e) pattern in a match statement?
It catches the error case and lets you handle or report the error stored in e.
Click to reveal answer
intermediate
Why is using match for error handling better than just unwrapping?
Because match lets you handle errors gracefully without crashing the program, unlike unwrap() which panics on errors.
Click to reveal answer
beginner
Write a simple match statement to handle a Result<String, &str> that prints the string on success or prints the error message on failure.
let result: Result<String, &str> = Ok("Hello".to_string());
match result {
Ok(text) => println!("Success: {}", text),
Err(e) => println!("Error: {}", e),
}
Click to reveal answer
What does match do when used with a Result type in Rust?
AIt ignores errors silently.
BIt automatically fixes errors.
CIt converts errors into success values.
DIt lets you handle both success and error cases separately.
✗ Incorrect
The match statement lets you write code for both Ok and Err cases explicitly.
Which pattern matches the error variant in a Result?
AOk(value)
BSome(value)
CErr(e)
DNone
✗ Incorrect
Err(e) matches the error variant and binds the error to e.
What happens if you use unwrap() on an Err value?
APanics and crashes the program.
BConverts error to success automatically.
CIgnores the error and continues.
DReturns the error value.
✗ Incorrect
unwrap() panics if the Result is an Err, causing the program to crash.
In a match statement, how do you bind the success value of a Result to a variable?
AOk(value)
BErr(value)
CSome(value)
DNone
✗ Incorrect
The pattern Ok(value) binds the success value to value.
Why is handling errors with match considered good practice?
ABecause it hides errors from users.
BBecause it forces you to handle all possible cases explicitly.
CBecause it makes the program run faster.
DBecause it automatically fixes bugs.
✗ Incorrect
match requires you to handle every case, making your program more reliable.
Explain how to use match to handle a Result in Rust.
Think about how you separate success and error cases.
You got /5 concepts.
Describe why using match for error handling is safer than using unwrap().
Consider what happens when an error occurs.
You got /4 concepts.
Practice
(1/5)
1. What does the match statement do when used with a Result type in Rust?
easy
A. It ignores errors and continues execution.
B. It automatically fixes errors in the code.
C. It converts the result into a string.
D. It checks if the result is Ok or Err and lets you handle each case.
Solution
Step 1: Understand the purpose of match with Result
The match statement is used to check the two possible states of a Result: Ok for success and Err for failure.
Step 2: Recognize handling of both cases
By matching on Ok and Err, you can write code to handle success and error separately, making the program safe and clear.
Final Answer:
It checks if the result is Ok or Err and lets you handle each case. -> Option D
Quick Check:
match with Result = handle Ok or Err [OK]
Hint: Remember: match splits Result into Ok or Err cases [OK]
Common Mistakes:
Thinking match fixes errors automatically
Assuming match converts Result to string
Ignoring error cases in match
2. Which of the following is the correct syntax to match a Result<T, E> named res and print "Success" if Ok?
easy
A. match res { Ok => println!("Success"), Err => println!("Error") }
B. match res { Ok(_) => println!("Success"), Err(_) => println!("Error") }
C. match res { Ok(x) println!("Success"), Err(e) println!("Error") }
D. match res { Ok(_) => println!("Error"), Err(_) => println!("Success") }
Solution
Step 1: Check pattern matching syntax
The correct syntax uses Ok(_) and Err(_) with arrows => to separate patterns from actions.
Step 2: Verify the printed messages
match res { Ok(_) => println!("Success"), Err(_) => println!("Error") } prints "Success" on Ok and "Error" on Err, which matches the question requirement.
Final Answer:
match res { Ok(_) => println!("Success"), Err(_) => println!("Error") } -> Option B
Quick Check:
Correct match syntax = match res { Ok(_) => println!("Success"), Err(_) => println!("Error") } [OK]
Hint: Use arrows => after patterns and parentheses for Ok/Err [OK]
Common Mistakes:
Omitting arrows => in match arms
Not using parentheses for Ok(_) or Err(_)
Swapping success and error messages
3. What will be the output of this Rust code?
let res: Result<i32, &str> = Err("fail");
match res {
Ok(val) => println!("Value: {}", val),
Err(e) => println!("Error: {}", e),
}
medium
A. Error: fail
B. Value: 0
C. Value: fail
D. No output
Solution
Step 1: Identify the Result variant
The variable res is assigned Err("fail"), so it represents an error.
Step 2: Match arms execution
The match will go to the Err(e) arm and print "Error: fail".
Final Answer:
Error: fail -> Option A
Quick Check:
Err variant prints error message [OK]
Hint: Check if Result is Ok or Err to predict output [OK]
Common Mistakes:
Confusing Err with Ok variant
Expecting value instead of error message
Ignoring the match arms
4. Find the error in this Rust code snippet:
let res: Result<i32, &str> = Ok(10);
match res {
Ok(val) => println!("Value: {}", val),
Err => println!("Error occurred"),
}
medium
A. Missing parentheses after Err in match arm
B. Missing semicolon after println! macro
C. Ok variant should be Err
D. Result type is incorrect
Solution
Step 1: Check match arm patterns
In Rust, match arms for Result must pattern match with parentheses, so Err should be Err(_) or Err(e).
Step 2: Identify the syntax error
The code uses Err without parentheses, which is invalid syntax and causes a compile error.
Final Answer:
Missing parentheses after Err in match arm -> Option A
Quick Check:
Err pattern needs parentheses () [OK]
Hint: Always use parentheses for Err(_) or Ok(_) in match [OK]
Common Mistakes:
Writing Err without parentheses
Confusing semicolon placement
Changing Result type unnecessarily
5. You want to write a function that returns Result<i32, String>. Inside, you try to parse a string to an integer. Using match, how do you handle the parsing result to return Ok(number) if successful or Err with a custom message if it fails?
hard
A. match s.parse() { Ok(n) => Err(n), Err(e) => Ok(e.to_string()) }
B. if s.parse().is_ok() { Ok(s.parse().unwrap()) } else { Err("Parse failed") }
C. match s.parse() { Ok(n) => Ok(n), Err(_) => Err(String::from("Parse failed")) }
D. match s.parse() { Ok(n) => n, Err(_) => "Parse failed" }
Solution
Step 1: Understand parse returns Result
The parse() method returns Result<T, E>, so matching on it lets us handle success and failure.
Step 2: Return matching Result from function
On Ok(n), return Ok(n) to propagate success. On Err(_), return Err with a custom error message as a String.
Final Answer:
match s.parse() { Ok(n) => Ok(n), Err(_) => Err(String::from("Parse failed")) } -> Option C
Quick Check:
Match parse Result to return Ok or custom Err [OK]
Hint: Return Ok(n) on success, Err(custom message) on failure [OK]
Common Mistakes:
Swapping Ok and Err returns
Returning raw values instead of Result
Using string literals instead of String in Err
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.