completed 04 traits

This commit is contained in:
2025-03-02 08:27:03 +05:30
parent 10fea65893
commit 7e998862fa
3 changed files with 42 additions and 1 deletions

View File

@@ -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,

View File

@@ -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 }

View File

@@ -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 {