Hone logo
Hone
Problems

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 CustomResult must be generic over two types: T (the success type) and E (the error type).
  • Ok and Err Variants: The CustomResult must have two variants: Ok(T) representing a successful result and Err(E) representing an error.
  • is_ok() method: A method that returns true if the CustomResult is an Ok variant, and false otherwise.
  • is_err() method: A method that returns true if the CustomResult is an Err variant, and false otherwise.
  • unwrap() method: A method that returns the contained value if the CustomResult is an Ok variant. If it's an Err variant, it should panic with the error message "Result is an error".
  • unwrap_or(default_value) method: A method that returns the contained value if the CustomResult is an Ok variant. If it's an Err variant, it should return the provided default_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 E type for Err should be able to be converted to a String for the panic message.
  • The unwrap() method must panic if the CustomResult is an Err variant.
  • The unwrap_or() method should accept a default value of the same type as T.
  • The code should be well-formatted and idiomatic Rust.

Notes

  • Consider using an enum to represent the Ok and Err variants.
  • Think about how to handle the generic types T and E within the CustomResult type.
  • The panic! macro is useful for signaling errors in the unwrap() method.
  • This is a simplified version of Result. The standard library Result has more features (like map, and_then, etc.), but this challenge focuses on the core concepts.
Loading editor...
rust