start 04 traits, completed upto 10
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
fn intro() -> &'static str {
|
fn intro() -> &'static str {
|
||||||
// TODO: fix me 👇
|
// TODO: fix me 👇
|
||||||
"I'm ready to __!"
|
"I'm ready to learn about traits!"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -3,6 +3,22 @@
|
|||||||
//
|
//
|
||||||
// Then implement the trait for `u32` and `i32`.
|
// 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
// Look at the compiler error to get familiar with what it looks like.
|
// 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.
|
// Then delete the code below and move on to the next exercise.
|
||||||
|
|
||||||
impl PartialEq for u32 {
|
// impl PartialEq for u32 {
|
||||||
fn eq(&self, _other: &Self) -> bool {
|
// fn eq(&self, _other: &Self) -> bool {
|
||||||
todo!()
|
// todo!()
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ struct Ticket {
|
|||||||
|
|
||||||
// TODO: Implement the `PartialEq` trait for `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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
// print both sides of the comparison to the terminal.
|
// 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!
|
// If the compared type doesn't implement `Debug`, it doesn't know how to represent them!
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct Ticket {
|
struct Ticket {
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
|
|||||||
@@ -5,8 +5,13 @@
|
|||||||
// different _semantics_. We'll cover those differences later in the course when we talk about ordered
|
// different _semantics_. We'll cover those differences later in the course when we talk about ordered
|
||||||
// collections (e.g. BTreeMap).
|
// collections (e.g. BTreeMap).
|
||||||
|
|
||||||
|
use std::cmp::PartialOrd;
|
||||||
|
use std::fmt::Debug;
|
||||||
/// Return the minimum of two values.
|
/// 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 {
|
if left <= right {
|
||||||
left
|
left
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -31,16 +31,16 @@ impl Ticket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn title(&self) -> &String {
|
pub fn title(&self) -> &str {
|
||||||
&self.title
|
&self.title.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn description(&self) -> &String {
|
pub fn description(&self) -> &str {
|
||||||
&self.description
|
&self.description.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn status(&self) -> &String {
|
pub fn status(&self) -> &str {
|
||||||
&self.status
|
&self.status.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ pub struct Ticket {
|
|||||||
|
|
||||||
impl Ticket {
|
impl Ticket {
|
||||||
pub fn title(&self) -> &str {
|
pub fn title(&self) -> &str {
|
||||||
todo!()
|
self.title.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn description(&self) -> &str {
|
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.
|
// 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.
|
// 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,
|
value: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<u32> for WrappingU32 {
|
||||||
|
fn from(value: u32) -> Self {
|
||||||
|
WrappingU32 { value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn example() {
|
fn example() {
|
||||||
let wrapping: WrappingU32 = 42.into();
|
let wrapping: WrappingU32 = 42.into();
|
||||||
let wrapping = WrappingU32::from(42);
|
let wrapping = WrappingU32::from(42);
|
||||||
|
|||||||
@@ -13,6 +13,28 @@
|
|||||||
// You don't have to though: it's perfectly okay to write three separate
|
// You don't have to though: it's perfectly okay to write three separate
|
||||||
// implementations manually. Venture further only if you're curious.
|
// 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Power;
|
use super::Power;
|
||||||
|
|||||||
Reference in New Issue
Block a user