mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 21:59:18 +00:00
conversions
This commit is contained in:
parent
06a541ce38
commit
3d2a78ec0d
@ -3,24 +3,22 @@
|
||||
// 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
|
||||
// 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()
|
||||
}
|
||||
|
||||
// Squares a number using as_mut(). Add the trait bound as is appropriate and
|
||||
// implement the function body.
|
||||
fn num_sq<T>(arg: &mut T) {
|
||||
???
|
||||
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
|
||||
*arg.as_mut() *= *arg.as_mut()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -35,10 +35,35 @@ 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 {
|
||||
if s == "" {
|
||||
Person::default()
|
||||
} else {
|
||||
s.split(",")
|
||||
.map(|x| x.into())
|
||||
.collect::<Vec<String>>()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<String>> for Person {
|
||||
fn from(s: Vec<String>) -> Self {
|
||||
if s.len() != 2 {
|
||||
return Person::default();
|
||||
}
|
||||
let number = s[1].parse();
|
||||
let age;
|
||||
if number.is_err() || s[0] == "" {
|
||||
Person::default()
|
||||
} else {
|
||||
age = number.unwrap();
|
||||
Person {
|
||||
name: s[0].clone(),
|
||||
age: age,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,24 @@ enum ParsePersonError {
|
||||
impl FromStr for Person {
|
||||
type Err = ParsePersonError;
|
||||
fn from_str(s: &str) -> Result<Person, Self::Err> {
|
||||
if s == "" {
|
||||
Err(ParsePersonError::Empty)
|
||||
} else {
|
||||
let s = s.split(",").map(|x| x.into()).collect::<Vec<String>>();
|
||||
if s.len() != 2 {
|
||||
return Err(ParsePersonError::BadLen);
|
||||
} else if s[0] == "" {
|
||||
return Err(ParsePersonError::NoName);
|
||||
}
|
||||
let number = s[1].parse();
|
||||
if let Err(x) = number {
|
||||
return Err(ParsePersonError::ParseInt(x));
|
||||
}
|
||||
Ok(Person {
|
||||
name: s[0].clone(),
|
||||
age: number.unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
@ -38,6 +36,16 @@ enum IntoColorError {
|
||||
impl TryFrom<(i16, i16, i16)> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
|
||||
for i in [tuple.0, tuple.1, tuple.2] {
|
||||
if i < 0 || i > 255 {
|
||||
return Err(IntoColorError::IntConversion);
|
||||
}
|
||||
}
|
||||
Ok(Color {
|
||||
red: tuple.0 as u8,
|
||||
green: tuple.1 as u8,
|
||||
blue: tuple.2 as u8,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,6 +53,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> {
|
||||
Color::try_from((arr[0], arr[1], arr[2]))
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,6 +61,11 @@ impl TryFrom<[i16; 3]> for Color {
|
||||
impl TryFrom<&[i16]> for Color {
|
||||
type Error = IntoColorError;
|
||||
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
|
||||
if slice.len() != 3 {
|
||||
Err(IntoColorError::BadLen)
|
||||
} else {
|
||||
Color::try_from((slice[0], slice[1], slice[2]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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…
x
Reference in New Issue
Block a user