From 10fea6589345b23e0374b2e556492f8b80ac6bb7 Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Sat, 1 Mar 2025 15:00:03 +0530 Subject: [PATCH] start 04 traits, completed upto 10 --- exercises/04_traits/00_intro/src/lib.rs | 2 +- exercises/04_traits/01_trait/src/lib.rs | 16 ++++++++++++++ exercises/04_traits/02_orphan_rule/src/lib.rs | 10 ++++----- .../03_operator_overloading/src/lib.rs | 8 ++++++- exercises/04_traits/04_derive/src/lib.rs | 2 +- .../04_traits/05_trait_bounds/src/lib.rs | 7 +++++- exercises/04_traits/06_str_slice/src/lib.rs | 12 +++++----- exercises/04_traits/07_deref/src/lib.rs | 4 ++-- exercises/04_traits/08_sized/src/lib.rs | 2 +- exercises/04_traits/09_from/src/lib.rs | 6 +++++ .../04_traits/10_assoc_vs_generic/src/lib.rs | 22 +++++++++++++++++++ 11 files changed, 73 insertions(+), 18 deletions(-) diff --git a/exercises/04_traits/00_intro/src/lib.rs b/exercises/04_traits/00_intro/src/lib.rs index 9513649..1b0f8ee 100644 --- a/exercises/04_traits/00_intro/src/lib.rs +++ b/exercises/04_traits/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to learn about traits!" } #[cfg(test)] diff --git a/exercises/04_traits/01_trait/src/lib.rs b/exercises/04_traits/01_trait/src/lib.rs index 258eac5..9ebfe5f 100644 --- a/exercises/04_traits/01_trait/src/lib.rs +++ b/exercises/04_traits/01_trait/src/lib.rs @@ -3,6 +3,22 @@ // // Then implement the trait for `u32` and `i32`. +trait IsEven { + fn is_even(self) -> bool; +} + +impl IsEven for u32 { + fn is_even(self) -> bool { + self % 2 == 0 + } +} + +impl IsEven for i32 { + fn is_even(self) -> bool { + self % 2 == 0 + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/04_traits/02_orphan_rule/src/lib.rs b/exercises/04_traits/02_orphan_rule/src/lib.rs index b707c10..e862fa8 100644 --- a/exercises/04_traits/02_orphan_rule/src/lib.rs +++ b/exercises/04_traits/02_orphan_rule/src/lib.rs @@ -4,8 +4,8 @@ // Look at the compiler error to get familiar with what it looks like. // Then delete the code below and move on to the next exercise. -impl PartialEq for u32 { - fn eq(&self, _other: &Self) -> bool { - todo!() - } -} +// impl PartialEq for u32 { +// fn eq(&self, _other: &Self) -> bool { +// todo!() +// } +// } diff --git a/exercises/04_traits/03_operator_overloading/src/lib.rs b/exercises/04_traits/03_operator_overloading/src/lib.rs index b75c0f1..efb0aa7 100644 --- a/exercises/04_traits/03_operator_overloading/src/lib.rs +++ b/exercises/04_traits/03_operator_overloading/src/lib.rs @@ -8,7 +8,13 @@ struct Ticket { // TODO: Implement the `PartialEq` trait for `Ticket`. -impl PartialEq for Ticket {} +impl PartialEq for Ticket { + fn eq(&self, other: &Self) -> bool { + self.description == other.description + && self.status == other.status + && self.title == other.title + } +} #[cfg(test)] mod tests { diff --git a/exercises/04_traits/04_derive/src/lib.rs b/exercises/04_traits/04_derive/src/lib.rs index 0a74a26..d39d6c5 100644 --- a/exercises/04_traits/04_derive/src/lib.rs +++ b/exercises/04_traits/04_derive/src/lib.rs @@ -8,7 +8,7 @@ // print both sides of the comparison to the terminal. // If the compared type doesn't implement `Debug`, it doesn't know how to represent them! -#[derive(PartialEq)] +#[derive(PartialEq, Debug)] struct Ticket { title: String, description: String, diff --git a/exercises/04_traits/05_trait_bounds/src/lib.rs b/exercises/04_traits/05_trait_bounds/src/lib.rs index 10f0eb4..76d7ca0 100644 --- a/exercises/04_traits/05_trait_bounds/src/lib.rs +++ b/exercises/04_traits/05_trait_bounds/src/lib.rs @@ -5,8 +5,13 @@ // different _semantics_. We'll cover those differences later in the course when we talk about ordered // collections (e.g. BTreeMap). +use std::cmp::PartialOrd; +use std::fmt::Debug; /// Return the minimum of two values. -pub fn min(left: T, right: T) -> T { +pub fn min(left: T, right: T) -> T +where + T: Debug + Clone + PartialOrd, +{ if left <= right { left } else { diff --git a/exercises/04_traits/06_str_slice/src/lib.rs b/exercises/04_traits/06_str_slice/src/lib.rs index 5bf6614..5420656 100644 --- a/exercises/04_traits/06_str_slice/src/lib.rs +++ b/exercises/04_traits/06_str_slice/src/lib.rs @@ -31,16 +31,16 @@ impl Ticket { } } - pub fn title(&self) -> &String { - &self.title + pub fn title(&self) -> &str { + &self.title.as_str() } - pub fn description(&self) -> &String { - &self.description + pub fn description(&self) -> &str { + &self.description.as_str() } - pub fn status(&self) -> &String { - &self.status + pub fn status(&self) -> &str { + &self.status.as_str() } } diff --git a/exercises/04_traits/07_deref/src/lib.rs b/exercises/04_traits/07_deref/src/lib.rs index c7a5c35..a21f9ef 100644 --- a/exercises/04_traits/07_deref/src/lib.rs +++ b/exercises/04_traits/07_deref/src/lib.rs @@ -12,11 +12,11 @@ pub struct Ticket { impl Ticket { pub fn title(&self) -> &str { - todo!() + self.title.trim() } pub fn description(&self) -> &str { - todo!() + self.description.trim() } } diff --git a/exercises/04_traits/08_sized/src/lib.rs b/exercises/04_traits/08_sized/src/lib.rs index a406fc5..0c8e7ee 100644 --- a/exercises/04_traits/08_sized/src/lib.rs +++ b/exercises/04_traits/08_sized/src/lib.rs @@ -3,5 +3,5 @@ pub fn example() { // via `std::mem::size_of` will result in a compile-time error. // // TODO: Comment out the following line and move on to the next exercise. - std::mem::size_of::(); + std::mem::size_of::<&str>(); } diff --git a/exercises/04_traits/09_from/src/lib.rs b/exercises/04_traits/09_from/src/lib.rs index cc6f5b1..b7995d2 100644 --- a/exercises/04_traits/09_from/src/lib.rs +++ b/exercises/04_traits/09_from/src/lib.rs @@ -4,6 +4,12 @@ pub struct WrappingU32 { value: u32, } +impl From for WrappingU32 { + fn from(value: u32) -> Self { + WrappingU32 { value } + } +} + fn example() { let wrapping: WrappingU32 = 42.into(); let wrapping = WrappingU32::from(42); diff --git a/exercises/04_traits/10_assoc_vs_generic/src/lib.rs b/exercises/04_traits/10_assoc_vs_generic/src/lib.rs index 84f3e7b..490713a 100644 --- a/exercises/04_traits/10_assoc_vs_generic/src/lib.rs +++ b/exercises/04_traits/10_assoc_vs_generic/src/lib.rs @@ -13,6 +13,28 @@ // You don't have to though: it's perfectly okay to write three separate // implementations manually. Venture further only if you're curious. +pub trait Power { + fn power(&self, power: T) -> Self; +} + +impl Power for u32 { + fn power(&self, power: u16) -> Self { + self.pow(power as u32) + } +} + +impl Power for u32 { + fn power(&self, power: u32) -> Self { + self.pow(power) + } +} + +impl Power<&u32> for u32 { + fn power(&self, power: &u32) -> Self { + self.pow(power.clone()) + } +} + #[cfg(test)] mod tests { use super::Power;