diff --git a/exercises/04_traits/11_clone/src/lib.rs b/exercises/04_traits/11_clone/src/lib.rs index 4fbbe47..8d93806 100644 --- a/exercises/04_traits/11_clone/src/lib.rs +++ b/exercises/04_traits/11_clone/src/lib.rs @@ -2,9 +2,10 @@ // to get the code to compile. pub fn summary(ticket: Ticket) -> (Ticket, Summary) { - (ticket, ticket.summary()) + (ticket.clone(), ticket.summary()) } +#[derive(Clone)] pub struct Ticket { pub title: String, pub description: String, diff --git a/exercises/04_traits/12_copy/src/lib.rs b/exercises/04_traits/12_copy/src/lib.rs index d74720b..4175320 100644 --- a/exercises/04_traits/12_copy/src/lib.rs +++ b/exercises/04_traits/12_copy/src/lib.rs @@ -1,10 +1,28 @@ // TODO: implement the necessary traits to make the test compile and pass. // You *can't* modify the test. +use std::ops::Add; + +#[derive(Clone, Copy, Debug)] pub struct WrappingU32 { value: u32, } +impl PartialEq for WrappingU32 { + fn eq(&self, other: &Self) -> bool { + self.value == other.value + } +} + +impl Add for WrappingU32 { + type Output = WrappingU32; + fn add(self, rhs: Self) -> Self::Output { + WrappingU32 { + value: self.value.wrapping_add(rhs.value), + } + } +} + impl WrappingU32 { pub fn new(value: u32) -> Self { Self { value } diff --git a/exercises/04_traits/13_drop/src/lib.rs b/exercises/04_traits/13_drop/src/lib.rs index 2124d34..3a6d2ff 100644 --- a/exercises/04_traits/13_drop/src/lib.rs +++ b/exercises/04_traits/13_drop/src/lib.rs @@ -1,6 +1,28 @@ // TODO: implement a so-called "Drop bomb": a type that panics when dropped // unless a certain operation has been performed on it. // You can see the expected API in the tests below. +#[derive(Debug, Clone)] +pub struct DropBomb { + a: bool, +} + +impl DropBomb { + fn new() -> DropBomb { + DropBomb { a: false } + } + + fn defuse(&mut self) { + self.a = true; + } +} + +impl Drop for DropBomb { + fn drop(&mut self) { + if !self.a { + panic!("Not Defused!!") + } + } +} #[cfg(test)] mod tests {