All the exercises complete

This commit is contained in:
Emre AYDIN 2021-01-13 17:50:01 +03:00
parent 84e88274fb
commit 9d5237824b
8 changed files with 69 additions and 20 deletions

View file

@ -6,12 +6,10 @@
// check clippy's suggestions from the output to solve the exercise. // check clippy's suggestions from the output to solve the exercise.
// Execute `rustlings hint clippy1` for hints :) // Execute `rustlings hint clippy1` for hints :)
// I AM NOT DONE
fn main() { fn main() {
let x = 1.2331f64; let x = 1.2331f64;
let y = 1.2332f64; let y = 1.2332f64;
if y != x { if (y - x).abs() != 0.0 {
println!("Success!"); println!("Success!");
} }
} }

View file

@ -1,12 +1,10 @@
// clippy2.rs // clippy2.rs
// Make me compile! Execute `rustlings hint clippy2` for hints :) // Make me compile! Execute `rustlings hint clippy2` for hints :)
// I AM NOT DONE
fn main() { fn main() {
let mut res = 42; let mut res = 42;
let option = Some(12); let option = Some(12);
for x in option { if let Some(x) = option {
res += x; res += x;
} }
println!("{}", res); println!("{}", res);

View file

@ -2,17 +2,15 @@
// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html // 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. // 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 // Obtain the number of bytes (not characters) in the given argument
// Add the AsRef trait appropriately as a trait bound // 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() arg.as_ref().as_bytes().len()
} }
// Obtain the number of characters (not bytes) in the given argument // Obtain the number of characters (not bytes) in the given argument
// Add the AsRef trait appropriately as a trait bound // 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() arg.as_ref().chars().count()
} }

View file

@ -37,6 +37,18 @@ impl Default for Person {
impl From<&str> for Person { impl From<&str> for Person {
fn from(s: &str) -> Person { fn from(s: &str) -> Person {
let person: Vec<&str> = s.split(",").collect();
match person.len() {
2 => match person[0].to_owned().len() {
0 => Err(String::from("Error")),
_ => Ok(Person {
name: person[0].to_owned(),
age: person[1].parse::<usize>().unwrap(),
}),
},
_ => Err(String::from("Error")),
}
} }
} }

View file

@ -10,7 +10,6 @@ struct Person {
age: usize, age: usize,
} }
// I AM NOT DONE
// Steps: // Steps:
// 1. If the length of the provided string is 0, then return an error // 1. If the length of the provided string is 0, then return an error
// 2. Split the given string on the commas present in it // 2. Split the given string on the commas present in it
@ -23,6 +22,18 @@ struct Person {
impl FromStr for Person { impl FromStr for Person {
type Err = String; type Err = String;
fn from_str(s: &str) -> Result<Person, Self::Err> { fn from_str(s: &str) -> Result<Person, Self::Err> {
let person: Vec<&str> = s.split(",").collect();
match person.len() {
2 => match person[0].to_owned().len() {
0 => Err(String::from("Error")),
_ => Ok(Person {
name: person[0].to_owned(),
age: person[1].parse::<usize>().unwrap(),
}),
},
_ => Err(String::from("Error")),
}
} }
} }

View file

@ -11,8 +11,6 @@ struct Color {
blue: u8, blue: u8,
} }
// I AM NOT DONE
// Your task is to complete this implementation // Your task is to complete this implementation
// and return an Ok result of inner type Color. // and return an Ok result of inner type Color.
// You need to create an implementation for a tuple of three integers, // You need to create an implementation for a tuple of three integers,
@ -25,19 +23,56 @@ struct Color {
// Tuple implementation // Tuple implementation
impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<(i16, i16, i16)> for Color {
type Error = String; 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> {
let (r, g, b) = tuple;
if (0..=255).contains(&r) && (0..=255).contains(&g) && (0..=255).contains(&b) {
Ok(Color {
red: r as u8,
green: g as u8,
blue: b as u8,
})
} else {
Err(String::from("Error occured."))
}
}
} }
// Array implementation // Array implementation
impl TryFrom<[i16; 3]> for Color { impl TryFrom<[i16; 3]> for Color {
type Error = String; type Error = String;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {} fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
if (0..=255).contains(&arr[0]) && (0..=255).contains(&arr[1]) && (0..=255).contains(&arr[2])
{
Ok(Color {
red: arr[0] as u8,
green: arr[1] as u8,
blue: arr[2] as u8,
})
} else {
Err(String::from("Error occured."))
}
}
} }
// Slice implementation // Slice implementation
impl TryFrom<&[i16]> for Color { impl TryFrom<&[i16]> for Color {
type Error = String; 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("Length should be 3."))
} else if (0..=255).contains(&slice[0])
&& (0..=255).contains(&slice[1])
&& (0..=255).contains(&slice[2])
{
Ok(Color {
red: slice[0] as u8,
green: slice[1] as u8,
blue: slice[2] as u8,
})
} else {
Err(String::from("Error occured."))
}
}
} }
fn main() { fn main() {

View file

@ -5,11 +5,9 @@
// The goal is to make sure that the division does not fail to compile // The goal is to make sure that the division does not fail to compile
// and returns the proper type. // and returns the proper type.
// I AM NOT DONE
fn average(values: &[f64]) -> f64 { fn average(values: &[f64]) -> f64 {
let total = values.iter().fold(0.0, |a, b| a + b); let total = values.iter().fold(0.0, |a, b| a + b);
total / values.len() total / values.len() as f64
} }
fn main() { fn main() {

View file

@ -5,7 +5,6 @@
// Write a macro that passes the quiz! No hints this time, you can do it! // Write a macro that passes the quiz! No hints this time, you can do it!
// I AM NOT DONE
#[macro_use] #[macro_use]
mod macros { mod macros {
macro_rules! my_macro { macro_rules! my_macro {