Ejercicios 94/94
This commit is contained in:
parent
5ac6421498
commit
27661c5c6a
|
@ -3,25 +3,19 @@
|
|||
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
|
||||
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Obtain the number of bytes (not characters) in the given argument.
|
||||
// TODO: 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.
|
||||
// TODO: 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()
|
||||
}
|
||||
|
||||
// Squares a number using as_mut().
|
||||
// TODO: Add the appropriate trait bound.
|
||||
fn num_sq<T>(arg: &mut T) {
|
||||
// TODO: Implement the function body.
|
||||
???
|
||||
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
|
||||
*arg.as_mut() = *arg.as_mut() * *arg.as_mut();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -35,10 +35,16 @@ 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 vec: Vec<&str> = s.split(",").collect();
|
||||
let name = vec[0].trim();
|
||||
let age = vec[vec.len() - 1].parse::<usize>();
|
||||
if vec.len() == 2 && name.len() > 0 && age.is_ok() {
|
||||
Person { name: name.to_string(), age: age.unwrap() }
|
||||
} else {
|
||||
Person::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,8 +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
|
||||
// 2. Split the given string on the commas present in it
|
||||
|
@ -46,6 +44,22 @@ enum ParsePersonError {
|
|||
impl FromStr for Person {
|
||||
type Err = ParsePersonError;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
let vec: Vec<&str> = s.split(",").collect();
|
||||
let name = vec[0].trim();
|
||||
let age = vec[vec.len() - 1].parse::<usize>();
|
||||
if vec.len() == 2 && name.len() > 0 && age.is_ok() {
|
||||
Ok(Person { name: name.to_string(), age: age.unwrap() })
|
||||
} else {
|
||||
if s.trim().len() == 0 {
|
||||
Err(ParsePersonError::Empty)
|
||||
} else if vec.len() != 2 {
|
||||
Err(ParsePersonError::BadLen)
|
||||
} else if name.len() == 0 {
|
||||
Err(ParsePersonError::NoName)
|
||||
} else {
|
||||
Err(ParsePersonError::ParseInt(age.unwrap_err()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,6 @@ enum IntoColorError {
|
|||
IntConversion,
|
||||
}
|
||||
|
||||
// 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,
|
||||
|
@ -34,10 +32,30 @@ enum IntoColorError {
|
|||
// but the slice implementation needs to check the slice length!
|
||||
// Also note that correct RGB color values must be integers in the 0..=255 range.
|
||||
|
||||
fn into_color(collection: Vec<i16>) -> Result<Color, IntoColorError> {
|
||||
let mut color = Color {red: 0, green: 0, blue: 0};
|
||||
if collection.len() < 3 { return Err(IntoColorError::BadLen) }
|
||||
for (i, num) in collection.iter().enumerate() {
|
||||
if *num >= 0 && *num <= 255 {
|
||||
let val = *num as u8;
|
||||
match i {
|
||||
0 => color.red = val,
|
||||
1 => color.green = val,
|
||||
2 => color.blue = val,
|
||||
_ => return Err(IntoColorError::BadLen),
|
||||
}
|
||||
} else {
|
||||
return Err(IntoColorError::IntConversion);
|
||||
}
|
||||
}
|
||||
Ok(color)
|
||||
}
|
||||
|
||||
// Tuple implementation
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
into_color(vec![tuple.0, tuple.1, tuple.2])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,6 +63,7 @@ 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> {
|
||||
into_color(arr.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +71,7 @@ impl TryFrom<[i16; 3]> for Color {
|
|||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
into_color(slice.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,11 +6,9 @@
|
|||
// 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() {
|
||||
|
|
Loading…
Reference in New Issue