mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 11:19:18 +00:00
update
This commit is contained in:
parent
f3c8716ada
commit
ae1c7a907c
@ -40,10 +40,18 @@ impl Default for Person {
|
|||||||
// If while parsing the age, something goes wrong, then return the default of
|
// If while parsing the age, something goes wrong, then return the default of
|
||||||
// Person Otherwise, then return an instantiated Person object with the results
|
// Person Otherwise, then return an instantiated Person object with the results
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
impl From<&str> for Person {
|
impl From<&str> for Person {
|
||||||
fn from(s: &str) -> Person {
|
fn from(s: &str) -> Person {
|
||||||
|
let v: Vec<&str> = s.split(',').collect();
|
||||||
|
if v.len() >= 2 && v[0].len() > 0 {
|
||||||
|
if let Ok(age) = v[1].parse::<usize>() {
|
||||||
|
return Person {
|
||||||
|
name: v[0].to_string(),
|
||||||
|
age,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Person::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,8 +31,6 @@ enum ParsePersonError {
|
|||||||
ParseInt(ParseIntError),
|
ParseInt(ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
// Steps:
|
// Steps:
|
||||||
// 1. If the length of the provided string is 0, an error should be returned
|
// 1. If the length of the provided string is 0, an error should be returned
|
||||||
// 2. Split the given string on the commas present in it
|
// 2. Split the given string on the commas present in it
|
||||||
@ -52,6 +50,24 @@ enum ParsePersonError {
|
|||||||
impl FromStr for Person {
|
impl FromStr for Person {
|
||||||
type Err = ParsePersonError;
|
type Err = ParsePersonError;
|
||||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||||
|
if s.is_empty() {
|
||||||
|
return Err(ParsePersonError::Empty);
|
||||||
|
}
|
||||||
|
let v: Vec<&str> = s.split(',').collect();
|
||||||
|
if v.len() == 2 {
|
||||||
|
if v[0].is_empty() {
|
||||||
|
return Err(ParsePersonError::NoName);
|
||||||
|
}
|
||||||
|
match v[1].parse::<usize>() {
|
||||||
|
Ok(age) => Ok(Person {
|
||||||
|
name: v[0].to_string(),
|
||||||
|
age,
|
||||||
|
}),
|
||||||
|
Err(err) => Err(ParsePersonError::ParseInt(err)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(ParsePersonError::BadLen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,11 +10,9 @@
|
|||||||
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn average(values: &[f64]) -> f64 {
|
fn average(values: &[f64]) -> f64 {
|
||||||
let total = values.iter().sum::<f64>();
|
let total = values.iter().sum::<f64>();
|
||||||
total / values.len()
|
total / values.len() as f64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user