feat: completed solutions
This commit is contained in:
@@ -5,20 +5,22 @@
|
||||
// Obtain the number of bytes (not characters) in the given argument
|
||||
// (`.len()` returns the number of bytes in a string).
|
||||
// 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().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) {
|
||||
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
|
||||
// TODO: Implement the function body.
|
||||
let val = *arg.as_mut();
|
||||
*arg.as_mut() = val * val;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -34,7 +34,18 @@ impl Default for Person {
|
||||
// 5. Parse the second element from the split operation into a `u8` as the age.
|
||||
// 6. If parsing the age fails, return the default of `Person`.
|
||||
impl From<&str> for Person {
|
||||
fn from(s: &str) -> Self {}
|
||||
fn from(s: &str) -> Self {
|
||||
match s.split(",").collect::<Vec<&str>>().as_slice() {
|
||||
[name, age] if (!name.is_empty() && !age.is_empty()) => match age.parse::<u8>() {
|
||||
Ok(age) => Person {
|
||||
name: name.to_string(),
|
||||
age,
|
||||
},
|
||||
Err(_) => Person::default(),
|
||||
},
|
||||
_ => Person::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -41,7 +41,19 @@ enum ParsePersonError {
|
||||
impl FromStr for Person {
|
||||
type Err = ParsePersonError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {}
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.split(",").collect::<Vec<&str>>().as_slice() {
|
||||
[name, age] if (!name.is_empty()) => match age.parse::<u8>() {
|
||||
Ok(age) => Ok(Person {
|
||||
name: name.to_string(),
|
||||
age,
|
||||
}),
|
||||
Err(err) => Err(ParsePersonError::ParseInt(err)),
|
||||
},
|
||||
["", _] => Err(ParsePersonError::NoName),
|
||||
_ => Err(ParsePersonError::BadLen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -28,14 +28,45 @@ enum IntoColorError {
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
match tuple {
|
||||
(red, green, blue)
|
||||
if (0..=255).contains(&red)
|
||||
&& (0..=255).contains(&green)
|
||||
&& (0..=255).contains(&blue) =>
|
||||
{
|
||||
Ok(Color {
|
||||
red: red as u8,
|
||||
green: green as u8,
|
||||
blue: blue as u8,
|
||||
})
|
||||
}
|
||||
_ => Err(IntoColorError::IntConversion),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Array implementation.
|
||||
impl TryFrom<[i16; 3]> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
|
||||
match arr.as_slice() {
|
||||
[red, green, blue] => {
|
||||
if (0..=255).contains(red) && (0..=255).contains(green) && (0..=255).contains(blue)
|
||||
{
|
||||
Ok(Color {
|
||||
red: *red as u8,
|
||||
green: *green as u8,
|
||||
blue: *blue as u8,
|
||||
})
|
||||
} else {
|
||||
Err(IntoColorError::IntConversion)
|
||||
}
|
||||
}
|
||||
_ => Err(IntoColorError::BadLen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Slice implementation.
|
||||
@@ -43,7 +74,23 @@ impl TryFrom<[i16; 3]> for Color {
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
match slice {
|
||||
[red, green, blue] => {
|
||||
if (0..=255).contains(red) && (0..=255).contains(green) && (0..=255).contains(blue)
|
||||
{
|
||||
Ok(Color {
|
||||
red: *red as u8,
|
||||
green: *green as u8,
|
||||
blue: *blue as u8,
|
||||
})
|
||||
} else {
|
||||
Err(IntoColorError::IntConversion)
|
||||
}
|
||||
}
|
||||
_ => Err(IntoColorError::BadLen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
fn average(values: &[f64]) -> f64 {
|
||||
let total = values.iter().sum::<f64>();
|
||||
// TODO: Make a conversion before dividing.
|
||||
total / values.len()
|
||||
total / values.len() as f64
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user