complete: conversions
This commit is contained in:
parent
4a08fff2b8
commit
8bb2cb01df
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,26 @@ 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 vecs:Vec<&str> = s.split(',').collect();
|
||||
if let [name, age] = *vecs {
|
||||
if name.is_empty() || age.is_empty() {
|
||||
Person::default()
|
||||
}else {
|
||||
if let Ok(age) = age.parse::<usize>() {
|
||||
Person{
|
||||
age,
|
||||
name: name.to_string(),
|
||||
}
|
||||
}else{
|
||||
Person::default()
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
Person::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,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
|
||||
|
@ -26,6 +24,19 @@ struct Person {
|
|||
impl FromStr for Person {
|
||||
type Err = Box<dyn error::Error>;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
let vecs:Vec<&str> = s.split(',').collect();
|
||||
if let [name, age] = *vecs {
|
||||
if name.is_empty() || age.is_empty() {
|
||||
Err("error".into())
|
||||
}else {
|
||||
Ok(Person{
|
||||
name: name.to_string(),
|
||||
age: age.parse::<usize>()?
|
||||
})
|
||||
}
|
||||
} else {
|
||||
Err("error".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,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,
|
||||
|
@ -26,19 +24,39 @@ struct Color {
|
|||
// Tuple implementation
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = Box<dyn error::Error>;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
|
||||
fn try_from((red, green, blue): (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
Color::try_from([red, green, blue])
|
||||
}
|
||||
}
|
||||
|
||||
// Array implementation
|
||||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = Box<dyn error::Error>;
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(rgb: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
let range = 0..=255;
|
||||
if rgb.iter().all(|v| range.contains(v)) {
|
||||
let [red, green, blue] = rgb;
|
||||
Ok(Color{
|
||||
red: red as u8,
|
||||
green: green as u8,
|
||||
blue: blue as u8
|
||||
})
|
||||
}else{
|
||||
Err("error".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Slice implementation
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = Box<dyn error::Error>;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
if let [red, green, blue] = *slice{
|
||||
Color::try_from([red, green, blue])
|
||||
}else{
|
||||
Err("error".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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