Hone logo
Hone
Problems

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:

  1. Implement for Option<T>: Create a function my_unwrap_option that takes an Option<T> and returns T.
  2. Implement for Result<T, E>: Create a function my_unwrap_result that takes a Result<T, E> and returns T.
  3. Panic on None/Err: If my_unwrap_option receives None, it must panic. If my_unwrap_result receives an Err, it must panic.
  4. Custom Panic Message: When panicking, the error message should be informative. For Option, the message should be "Called my_unwrap_option on a None value". For Result, the message should indicate the error value it encountered, similar to the standard library's unwrap_err() behavior: "Called my_unwrap_result on an Err value: {error_value}".

Expected Behavior:

  • If the input Option is Some(value), return value.
  • If the input Result is Ok(value), return value.
  • If the input Option is None, panic with the specified message.
  • If the input Result is Err(error), panic with the specified message including the error value.

Edge Cases:

  • The generic types T and E can be any valid Rust type.
  • Consider how the panic message for Err should display the error value. The Debug trait 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_option and my_unwrap_result must be generic over their contained types.
  • The my_unwrap_result function's error type E must implement the Debug trait to allow for formatted output in the panic message.
  • Your implementations should be placed within a mod named my_unwrap.

Notes

  • You will need to use pattern matching (match or if let) to inspect the Option and Result variants.
  • The panic! macro is used to cause a panic.
  • The {} placeholder in the panic! macro will use the Display trait by default for formatting. However, for the Err case in Result, you'll likely want to use the {:?} placeholder to leverage the Debug trait of the error type E.
  • Remember to bring the Option and Result types into scope if needed, though they are typically in the prelude.
Loading editor...
rust