From 4dfc11d74ab02ac5765ae5d08fb750c91f4744b4 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Fri, 2 Oct 2026 21:53:51 -0500 Subject: rustlings: move to rust/ --- rustlings/exercises/13_error_handling/errors5.rs | 56 ------------------------ 1 file changed, 56 deletions(-) delete mode 100644 rustlings/exercises/13_error_handling/errors5.rs (limited to 'rustlings/exercises/13_error_handling/errors5.rs') diff --git a/rustlings/exercises/13_error_handling/errors5.rs b/rustlings/exercises/13_error_handling/errors5.rs deleted file mode 100644 index 405f0dc..0000000 --- a/rustlings/exercises/13_error_handling/errors5.rs +++ /dev/null @@ -1,56 +0,0 @@ -// This exercise is an altered version of the `errors4` exercise. It uses some -// concepts that we won't get to until later in the course, like `Box` and the -// `From` trait. It's not important to understand them in detail right now, but -// you can read ahead if you like. For now, think of the `Box` type as -// an "I want anything that does ???" type. -// -// In short, this particular use case for boxes is for when you want to own a -// value and you care only that it is a type which implements a particular -// trait. To do so, the `Box` is declared as of type `Box` where -// `Trait` is the trait the compiler looks for on any value used in that -// context. For this exercise, that context is the potential errors which -// can be returned in a `Result`. - -use std::error::Error; -use std::fmt; - -#[derive(PartialEq, Debug)] -enum CreationError { - Negative, - Zero, -} - -// This is required so that `CreationError` can implement `Error`. -impl fmt::Display for CreationError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let description = match *self { - CreationError::Negative => "number is negative", - CreationError::Zero => "number is zero", - }; - f.write_str(description) - } -} - -impl Error for CreationError {} - -#[derive(PartialEq, Debug)] -struct PositiveNonzeroInteger(u64); - -impl PositiveNonzeroInteger { - fn new(value: i64) -> Result { - match value { - x if x < 0 => Err(CreationError::Negative), - 0 => Err(CreationError::Zero), - x => Ok(PositiveNonzeroInteger(x as u64)), - } - } -} - -// TODO: Add the correct return type `Result<(), Box>`. What can we -// use to describe both errors? Is there a trait which both errors implement? -fn main() -> Result<(), Box> { - let pretend_user_input = "42"; - let x: i64 = pretend_user_input.parse()?; - println!("output={:?}", PositiveNonzeroInteger::new(x)?); - Ok(()) -} -- cgit v1.2.3