Compare commits

..

5 Commits

Author SHA1 Message Date
7a1ca00837 solution to exercise 04_05 2024-07-13 17:39:46 +02:00
1789d43730 solution to exercise 04_04 2024-07-13 17:30:01 +02:00
604a5a20ae solution to exercise 04_03 2024-07-01 16:30:43 +02:00
0829174983 solution to exercise 04_02 2024-07-01 16:23:02 +02:00
ebe07acd1e solution to exercise 04_01 2024-07-01 16:15:42 +02:00
6 changed files with 29 additions and 10 deletions

View File

@@ -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)]

View File

@@ -3,6 +3,22 @@
// //
// Then implement the trait for `u32` and `i32`. // Then implement the trait for `u32` and `i32`.
pub 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::*;

View File

@@ -3,9 +3,3 @@
// a foreign type (`u32`, from `std`). // a foreign type (`u32`, from `std`).
// 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 {
fn eq(&self, _other: &Self) -> bool {
todo!()
}
}

View File

@@ -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.title == other.title
&& self.description == other.description
&& self.status == other.status
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@@ -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(Debug, PartialEq)]
struct Ticket { struct Ticket {
title: String, title: String,
description: String, description: String,

View File

@@ -6,7 +6,10 @@
// collections (e.g. BTreeMap). // collections (e.g. BTreeMap).
/// 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: Ord,
{
if left <= right { if left <= right {
left left
} else { } else {