Implementing a Custom Result Type in Rust
The Result type in Rust is fundamental for handling operations that might fail. This challenge asks you to implement a simplified version of Result to solidify your understanding of error handling, generics, and trait objects. Building this custom Result will help you appreciate the design choices behind Rust's standard library.
Problem Description
You are tasked with creating a custom Result type named CustomResult<T, E>. This type should represent the outcome of an operation that can either succeed with a value of type T or fail with an error of type E. Your CustomResult should provide methods to check if the result is Ok or Err, and to extract the contained value or error.
Key Requirements:
- Generic Types: The
CustomResultmust be generic over two types:T(the success type) andE(the error type). OkandErrVariants: TheCustomResultmust have two variants:Ok(T)representing a successful result andErr(E)representing an error.is_ok()method: A method that returnstrueif theCustomResultis anOkvariant, andfalseotherwise.is_err()method: A method that returnstrueif theCustomResultis anErrvariant, andfalseotherwise.unwrap()method: A method that returns the contained value if theCustomResultis anOkvariant. If it's anErrvariant, it should panic with the error message "Result is an error".unwrap_or(default_value)method: A method that returns the contained value if theCustomResultis anOkvariant. If it's anErrvariant, it should return the provideddefault_value.
Examples
Example 1:
Input: CustomResult::Ok(5)
Output: true (from is_ok())
Explanation: The CustomResult is an Ok variant containing the value 5.
Example 2:
Input: CustomResult::Err("Failed".to_string())
Output: false (from is_ok())
Output: true (from is_err())
Explanation: The CustomResult is an Err variant containing the error "Failed".
Example 3:
Input: let result: CustomResult<i32, String> = CustomResult::Ok(10);
Output: 10 (from unwrap())
Explanation: The result is an Ok variant containing 10, so unwrap() returns 10.
Example 4:
Input: let result: CustomResult<i32, String> = CustomResult::Err("Something went wrong".to_string());
Output: Panic with message "Result is an error" (from unwrap())
Explanation: The result is an Err variant, so unwrap() panics.
Example 5:
Input: let result: CustomResult<i32, String> = CustomResult::Err("Something went wrong".to_string());
Output: 0 (from unwrap_or(0))
Explanation: The result is an Err variant, so unwrap_or(0) returns 0.
Constraints
- The
Etype forErrshould be able to be converted to aStringfor the panic message. - The
unwrap()method must panic if theCustomResultis anErrvariant. - The
unwrap_or()method should accept a default value of the same type asT. - The code should be well-formatted and idiomatic Rust.
Notes
- Consider using an
enumto represent theOkandErrvariants. - Think about how to handle the generic types
TandEwithin theCustomResulttype. - The
panic!macro is useful for signaling errors in theunwrap()method. - This is a simplified version of
Result. The standard libraryResulthas more features (likemap,and_then, etc.), but this challenge focuses on the core concepts.