start 04 traits, completed upto 10
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
fn intro() -> &'static str {
|
||||
// TODO: fix me 👇
|
||||
"I'm ready to __!"
|
||||
"I'm ready to learn about traits!"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -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!()
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<T>(left: T, right: T) -> T {
|
||||
pub fn min<T>(left: T, right: T) -> T
|
||||
where
|
||||
T: Debug + Clone + PartialOrd,
|
||||
{
|
||||
if left <= right {
|
||||
left
|
||||
} else {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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::<str>();
|
||||
std::mem::size_of::<&str>();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,12 @@ pub struct WrappingU32 {
|
||||
value: u32,
|
||||
}
|
||||
|
||||
impl From<u32> for WrappingU32 {
|
||||
fn from(value: u32) -> Self {
|
||||
WrappingU32 { value }
|
||||
}
|
||||
}
|
||||
|
||||
fn example() {
|
||||
let wrapping: WrappingU32 = 42.into();
|
||||
let wrapping = WrappingU32::from(42);
|
||||
|
||||
@@ -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<T> {
|
||||
fn power(&self, power: T) -> Self;
|
||||
}
|
||||
|
||||
impl Power<u16> for u32 {
|
||||
fn power(&self, power: u16) -> Self {
|
||||
self.pow(power as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl Power<u32> 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;
|
||||
|
||||
Reference in New Issue
Block a user