Current Progress: 92/94
This commit is contained in:
@@ -35,10 +35,18 @@ impl Default for Person {
|
||||
// If while parsing the age, something goes wrong, then return the default of Person
|
||||
// Otherwise, then return an instantiated Person object with the results
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
impl From<&str> for Person {
|
||||
fn from(s: &str) -> Person {
|
||||
if s.len() == 0 { return Person::default(); }
|
||||
let v = s.split(",").collect::<Vec<&str>>();
|
||||
if v.len() != 2 { return Person::default(); }
|
||||
if v[0].is_empty() { return Person::default(); }
|
||||
if v[1].parse::<usize>().is_err() { return Person::default(); }
|
||||
Person{
|
||||
name: v[0].to_string(),
|
||||
age: v[1].parse::<usize>().unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ enum ParsePersonError {
|
||||
ParseInt(ParseIntError),
|
||||
}
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Steps:
|
||||
// 1. If the length of the provided string is 0, an error should be returned
|
||||
@@ -46,6 +45,15 @@ enum ParsePersonError {
|
||||
impl FromStr for Person {
|
||||
type Err = ParsePersonError;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
if s.len() == 0 { return Err(ParsePersonError::Empty); }
|
||||
let v = s.split(",").collect::<Vec<&str>>();
|
||||
if v.len() != 2 { return Err(ParsePersonError::BadLen); }
|
||||
if v[0].is_empty() { return Err(ParsePersonError::NoName); }
|
||||
if let Err(e) = v[1].parse::<usize>() { return Err(ParsePersonError::ParseInt(e)); }
|
||||
Ok(Person{
|
||||
name: v[0].to_string(),
|
||||
age: v[1].parse::<usize>().unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,11 @@ enum IntoColorError {
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
Ok(Color {
|
||||
red: tuple.0 as u8,
|
||||
green: tuple.1 as u8,
|
||||
blue: tuple.2 as u8,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +50,11 @@ impl TryFrom<(i16, i16, i16)> for Color {
|
||||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
Ok(Color {
|
||||
red: arr[0] as u8,
|
||||
green: arr[1] as u8,
|
||||
blue: arr[2] as u8,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +62,7 @@ impl TryFrom<[i16; 3]> for Color {
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,10 @@
|
||||
// and returns the proper type.
|
||||
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values.iter().sum::<f64>();
|
||||
total / values.len()
|
||||
total / values.len() as f64
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user