diff --git a/exercises/error_handling/errorsn.rs b/exercises/error_handling/errorsn.rs index cf605c5..aa2dcf8 100644 --- a/exercises/error_handling/errorsn.rs +++ b/exercises/error_handling/errorsn.rs @@ -20,12 +20,12 @@ use std::fmt; use std::io; // PositiveNonzeroInteger is a struct defined below the tests. -fn read_and_validate(b: &mut dyn io::BufRead) -> Result<PositiveNonzeroInteger, ???> { +fn read_and_validate(b: &mut dyn io::BufRead) -> Result<PositiveNonzeroInteger, Box<dyn error::Error>> { let mut line = String::new(); - b.read_line(&mut line); - let num: i64 = line.trim().parse(); - let answer = PositiveNonzeroInteger::new(num); - answer + b.read_line(&mut line)?; + let num: i64 = line.trim().parse()?; + let answer = PositiveNonzeroInteger::new(num)?; + Ok(answer) } // This is a test helper function that turns a &str into a BufReader. diff --git a/exercises/error_handling/result1.rs b/exercises/error_handling/result1.rs index b978001..7f5a8c2 100644 --- a/exercises/error_handling/result1.rs +++ b/exercises/error_handling/result1.rs @@ -1,7 +1,7 @@ // result1.rs // Make this test pass! Execute `rustlings hint result1` for hints :) -// I AM NOT DONE +use std::cmp::Ordering; #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,7 +14,18 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> { - Ok(PositiveNonzeroInteger(value as u64)) + match value.cmp(&0) { + Ordering::Less => Err(CreationError::Negative), + Ordering::Equal => Err(CreationError::Zero), + Ordering::Greater => Ok(PositiveNonzeroInteger(value as u64)), + } + // if value > 0 { + // Ok(PositiveNonzeroInteger(value as u64)) + // } else if value == 0 { + // Err(CreationError::Zero) + // } else { + // Err(CreationError::Negative) + // } } } diff --git a/exercises/option/option1.rs b/exercises/option/option1.rs index 2a6c2f7..16778a9 100644 --- a/exercises/option/option1.rs +++ b/exercises/option/option1.rs @@ -1,23 +1,21 @@ // option1.rs // Make me compile! Execute `rustlings hint option1` for hints -// I AM NOT DONE - // you can modify anything EXCEPT for this function's sig fn print_number(maybe_number: Option<u16>) { println!("printing: {}", maybe_number.unwrap()); } fn main() { - print_number(13); - print_number(99); + print_number(Some(13)); + print_number(Some(99)); - let mut numbers: [Option<u16>; 5]; + let mut numbers: [Option<u16>; 5] = Default::default(); for iter in 0..5 { let number_to_add: u16 = { ((iter * 5) + 2) / (4 * 16) }; - numbers[iter as usize] = number_to_add; + numbers[iter as usize] = Some(number_to_add); } } diff --git a/exercises/option/option2.rs b/exercises/option/option2.rs index a55f734..3bd8094 100644 --- a/exercises/option/option2.rs +++ b/exercises/option/option2.rs @@ -1,12 +1,10 @@ // option2.rs // Make me compile! Execute `rustlings hint option2` for hints -// I AM NOT DONE - fn main() { let optional_value = Some(String::from("rustlings")); // Make this an if let statement whose value is "Some" type - value = optional_value { + if let Some(value) = optional_value { println!("the value of optional value is: {}", value); } else { println!("The optional value doesn't contain anything!"); @@ -19,7 +17,7 @@ fn main() { // make this a while let statement - remember that vector.pop also adds another layer of Option<T> // You can stack `Option<T>`'s into while let and if let - value = optional_values_vec.pop() { - println!("current value: {}", value); + while let Some(value) = optional_values_vec.pop() { + println!("current value: {:?}", value); } }