Add conversions solutions
This commit is contained in:
parent
e50c35eb7c
commit
b66d86cce3
exercises/conversions
|
@ -2,17 +2,15 @@
|
|||
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
|
||||
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Obtain the number of bytes (not characters) in the given argument
|
||||
// Add the AsRef trait appropriately as a trait bound
|
||||
fn byte_counter<T>(arg: T) -> usize {
|
||||
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
|
||||
arg.as_ref().as_bytes().len()
|
||||
}
|
||||
|
||||
// Obtain the number of characters (not bytes) in the given argument
|
||||
// Add the AsRef trait appropriately as a trait bound
|
||||
fn char_counter<T>(arg: T) -> usize {
|
||||
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
|
||||
arg.as_ref().chars().count()
|
||||
}
|
||||
|
||||
|
|
|
@ -33,10 +33,20 @@ 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 {
|
||||
let parts: Vec<_> = s.split(',').collect();
|
||||
|
||||
if parts.len() != 2 || parts[0] == "" {
|
||||
Person::default()
|
||||
} else {
|
||||
let name = String::from(parts[0]);
|
||||
if let Ok(age) = parts[1].parse() {
|
||||
Person { name, age }
|
||||
} else {
|
||||
Person::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@ struct Person {
|
|||
age: usize,
|
||||
}
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Steps:
|
||||
// 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
|
||||
|
@ -25,6 +23,20 @@ struct Person {
|
|||
impl FromStr for Person {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
let parts: Vec<_> = s.split(',').collect();
|
||||
|
||||
if parts.len() != 2 {
|
||||
Err(String::from("two parts expected but not provided"))
|
||||
} else if parts[0] == "" {
|
||||
Err(String::from("name must be non-empty"))
|
||||
} else {
|
||||
let name = String::from(parts[0]);
|
||||
if let Ok(age) = parts[1].parse() {
|
||||
Ok(Person { name, age })
|
||||
} else {
|
||||
Err(String::from("age must be a number"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,6 @@ struct Color {
|
|||
blue: u8,
|
||||
}
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Your task is to complete this implementation
|
||||
// and return an Ok result of inner type Color.
|
||||
// You need to create an implementation for a tuple of three integers,
|
||||
|
@ -25,19 +23,49 @@ struct Color {
|
|||
// Tuple implementation
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = String;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
if let (Ok(red), Ok(green), Ok(blue)) =
|
||||
(tuple.0.try_into(), tuple.1.try_into(), tuple.2.try_into())
|
||||
{
|
||||
Ok(Color { red, green, blue })
|
||||
} else {
|
||||
Err(String::from("one or more numbers are out of range (0-255)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Array implementation
|
||||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = String;
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
if let (Ok(red), Ok(green), Ok(blue)) =
|
||||
(arr[0].try_into(), arr[1].try_into(), arr[2].try_into())
|
||||
{
|
||||
Ok(Color { red, green, blue })
|
||||
} else {
|
||||
Err(String::from("one or more numbers are out of range (0-255)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Slice implementation
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = String;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
if slice.len() != 3 {
|
||||
Err(String::from("incorrect slice length (expected 3 values)"))
|
||||
} else {
|
||||
if let (Ok(red), Ok(green), Ok(blue)) = (
|
||||
slice[0].try_into(),
|
||||
slice[1].try_into(),
|
||||
slice[2].try_into(),
|
||||
) {
|
||||
Ok(Color { red, green, blue })
|
||||
} else {
|
||||
Err(String::from("one or more numbers are out of range (0-255)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -5,11 +5,9 @@
|
|||
// The goal is to make sure that the division does not fail to compile
|
||||
// and returns the proper type.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values.iter().fold(0.0, |a, b| a + b);
|
||||
total / values.len()
|
||||
total / values.len() as f64
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
Loading…
Reference in a new issue