From 35cad2b17da78ee954b3b4a7b9cb656433974955 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Fri, 20 May 2022 15:36:59 -0400 Subject: [PATCH] generics1 done --- exercises/error_handling/errors5.rs | 6 ++---- exercises/error_handling/errors6.rs | 21 +++++++++++---------- exercises/generics/generics1.rs | 4 +--- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 365a8691..06f01c6b 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -4,14 +4,12 @@ // It won't compile right now! Why? // Execute `rustlings hint errors5` for hints! -// I AM NOT DONE - use std::error; use std::fmt; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), ParseIntError> { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); @@ -34,7 +32,7 @@ impl PositiveNonzeroInteger { match value { x if x < 0 => Err(CreationError::Negative), x if x == 0 => Err(CreationError::Zero), - x => Ok(PositiveNonzeroInteger(x as u64)) + x => Ok(PositiveNonzeroInteger(x as u64)), } } } diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 847a049a..b6470ab2 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -8,29 +8,30 @@ // Make these tests pass! Execute `rustlings hint errors6` for hints :) -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. #[derive(PartialEq, Debug)] enum ParsePosNonzeroError { Creation(CreationError), - ParseInt(ParseIntError) + ParseInt(ParseIntError), } impl ParsePosNonzeroError { // TODO: add another error conversion function here. + fn from_creation(err: CreationError) -> ParsePosNonzeroError { + ParsePosNonzeroError::Creation(err) + } + fn from_parse_int(err: ParseIntError) -> ParsePosNonzeroError { + ParsePosNonzeroError::ParseInt(err) + } } -fn parse_pos_nonzero(s: &str) - -> Result -{ +fn parse_pos_nonzero(s: &str) -> Result { // TODO: change this to return an appropriate error instead of panicking // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); - PositiveNonzeroInteger::new(x) - .map_err(ParsePosNonzeroError::from_creation) + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parse_int)?; + PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) } // Don't change anything below this line. @@ -49,7 +50,7 @@ impl PositiveNonzeroInteger { match value { x if x < 0 => Err(CreationError::Negative), x if x == 0 => Err(CreationError::Zero), - x => Ok(PositiveNonzeroInteger(x as u64)) + x => Ok(PositiveNonzeroInteger(x as u64)), } } } diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs index f93e64a0..59432e08 100644 --- a/exercises/generics/generics1.rs +++ b/exercises/generics/generics1.rs @@ -3,9 +3,7 @@ // Execute `rustlings hint generics1` for hints! -// I AM NOT DONE - fn main() { - let mut shopping_list: Vec = Vec::new(); + let mut shopping_list: Vec<&str> = Vec::new(); shopping_list.push("milk"); }