Implementing unwrap for Option and Result in Rust
In Rust, Option<T> and Result<T, E> are fundamental enums used to handle the presence or absence of a value, and the success or failure of an operation, respectively. The .unwrap() method is a common way to extract the contained value directly, but it panics if the Option is None or the Result is Err. This challenge focuses on understanding how .unwrap() works and implementing a similar behavior.
Problem Description
Your task is to implement a function that mimics the behavior of the .unwrap() method for Rust's Option<T> and Result<T, E> types. This function should take an Option or a Result as input and return the contained value if it's Some or Ok. If the input is None or Err, the function should panic with a specific error message.
Key Requirements:
- Implement for
Option<T>: Create a functionmy_unwrap_optionthat takes anOption<T>and returnsT. - Implement for
Result<T, E>: Create a functionmy_unwrap_resultthat takes aResult<T, E>and returnsT. - Panic on
None/Err: Ifmy_unwrap_optionreceivesNone, it must panic. Ifmy_unwrap_resultreceives anErr, it must panic. - Custom Panic Message: When panicking, the error message should be informative. For
Option, the message should be "Calledmy_unwrap_optionon aNonevalue". ForResult, the message should indicate the error value it encountered, similar to the standard library'sunwrap_err()behavior: "Calledmy_unwrap_resulton anErrvalue: {error_value}".
Expected Behavior:
- If the input
OptionisSome(value), returnvalue. - If the input
ResultisOk(value), returnvalue. - If the input
OptionisNone, panic with the specified message. - If the input
ResultisErr(error), panic with the specified message including theerrorvalue.
Edge Cases:
- The generic types
TandEcan be any valid Rust type. - Consider how the panic message for
Errshould display the error value. TheDebugtrait will be useful here.
Examples
Example 1: Option - Some
Input: Some(42)
Output: 42
Explanation: The function receives Some(42), so it extracts and returns the inner value 42.
Example 2: Option - None
Input: None
Output: Panics with the message "Called my_unwrap_option on a None value"
Explanation: The function receives None, triggering a panic with the specified error message.
Example 3: Result - Ok
Input: Ok("Success!")
Output: "Success!"
Explanation: The function receives Ok("Success!"), so it extracts and returns the inner value "Success!".
Example 4: Result - Err
Input: Err(std::io::ErrorKind::NotFound)
Output: Panics with the message "Called my_unwrap_result on an Err value: NotFound"
Explanation: The function receives Err(std::io::ErrorKind::NotFound), triggering a panic with the specified error message including the error variant.
Example 5: Result - Err with custom error
Input: Err("File not found")
Output: Panics with the message "Called my_unwrap_result on an Err value: \"File not found\""
Explanation: The function receives Err("File not found"), triggering a panic with the specified error message including the string error.
Constraints
- The functions
my_unwrap_optionandmy_unwrap_resultmust be generic over their contained types. - The
my_unwrap_resultfunction's error typeEmust implement theDebugtrait to allow for formatted output in the panic message. - Your implementations should be placed within a
modnamedmy_unwrap.
Notes
- You will need to use pattern matching (
matchorif let) to inspect theOptionandResultvariants. - The
panic!macro is used to cause a panic. - The
{}placeholder in thepanic!macro will use theDisplaytrait by default for formatting. However, for theErrcase inResult, you'll likely want to use the{:?}placeholder to leverage theDebugtrait of the error typeE. - Remember to bring the
OptionandResulttypes into scope if needed, though they are typically in the prelude.