Santa stomped into Bernard’s workshop, snatching a mug of cocoa off the table without asking. He squinted at the code from yesterday glowing on the monitor. “Ah, there it is! The Wrapped
status. Beautiful work, Bernard. Gifts wrapped and ready to go.”
Bernard gave a tired nod, his tools still in hand. “Yeah, it’s done. So what now?”
Santa’s eyes gleamed as he set the mug down. “Now… we distribute those gifts. And I know exactly how. Bernard, I need a struct. A unit struct. Call it Santa
.”
Bernard sighed. “Go on…”
“One method,” Santa said, his hands animated like he was pitching a startup. “Two generics: one for the recipients—kids, elves, reindeer. The other for the gifts—KidsGift
, ElvesGift
, ReindeerGift
.”
“Two generics?” Bernard raised an eyebrow. “Why not just one?”
Santa grinned, his beard twitching with excitement. “Trait bounds, Bernard. Nothing gets my sleigh flying like a beautifully constrained generic. where clauses are Christmas magic.”
Bernard groaned. “Fine. Two generics. Single method. Anything else?”
Santa waved dismissively. “You know the drill—keep it under wraps. No one can know until it’s perfect.” He leaned in closer, eyes twinkling. “And by perfect, I mean it better compile the first time.”
As Santa vanished back into the snowy chaos, Bernard muttered, “He loves trait bounds more than Christmas itself.”
It's clear that Santa is now obsessed with trait bounds, so we're gonna see a lot of it here.
Here's what you gotta do
Giftable
for entities that can receive gifts. Kid
, Elf
, and Reindeer
should implement this trait.Giftable
should have a method named receive_gift
it should set the gifted
field to true
.Gift
trait implementation for KidsGift
, ElvesGift
, and ReindeerGift
to include a method is_wrapped()
which returns a bool
to check if the gift is wrapped.Santa
struct to have a method named give_gift
that accepts two arguments: a recipient which could be any of Kid
, Elf
, or Reindeer
, and a gift which could be any of KidsGift
, ElvesGift
, or ReindeerGift
.Result<(), Box<dyn Error>>
, if the gift is not wrapped, return an error message.You can also have a look at the main
function at the end of the code to see how it works in action.
If you're stuck or need a starting point, here are some hints to help you along the way!
receive_gift
method should take a mutable reference to the self
object since it mutates a value.
pub trait Giftable {
fn receive_gift(&mut self);
}
impl Santa {
pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>>
where
R: Giftable,
G: Gift,
{
// your code here
}
}
// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.use std::{error::Error, fmt};pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Giftable, U: Gift>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); } else { return Result::Err("Not wrapped".into()); } Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() == false { return Result::Err("asd".into()); } recipient.receive_gift(); return Result::Ok(()); }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { return self.is_wrapped; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { return self.is_wrapped; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { return self.is_wrapped; }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() == false { return Result::Err("asd".into()); } recipient.receive_gift(); return Result::Ok(()); }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Giftable { fn receive_gift(&mut self);}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;#[derive(Debug)]pub struct WrappingError;impl std::error::Error for WrappingError {}impl fmt::Display for WrappingError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Gift not wrapped",) }}impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn std::error::Error>> { match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err(Box::new(WrappingError)) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.#[derive(Clone)]pub struct Kid { pub name: String, pub gifted: bool,}#[derive(Clone)]pub struct Reindeer { pub name: String, pub gifted: bool,}#[derive(Clone)]pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() == false { return Err("Gift must be wrapped before giving!".into()); } // Fix: Update the recipient to mark them as gifted recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { if !gift.is_wrapped() { Err("Gift is not wrapped".into()) } else { recipient.receive_gift(); Ok(()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn recieve_gift(&mut self);}impl Giftable for Kid{ fn recieve_gift(&mut self){ self.gifted = true; }}impl Giftable for Elf{ fn recieve_gift(&mut self){ self.gifted = true; }}impl Giftable for Reindeer{ fn recieve_gift(&mut self){ self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool{ return self.is_wrapped; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool{ return self.is_wrapped; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool{ return self.is_wrapped; }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift,{ if(!gift.is_wrapped()){ Err("Gift not wrapped")?; } recipient.recieve_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut dyn Giftable, gift: &dyn Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { Err("error".into()) } else { recipient.receive_gift(); Ok(()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}#[derive(Debug)]struct GiftError;impl Error for GiftError {}impl fmt::Display for GiftError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Wystąpił GiftError") }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { Err(Box::new(GiftError)) } else { recipient.receive_gift(); Ok(()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::{error::Error, fmt};// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true } // Update implementation}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; } // Update implementation}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; } // Update implementation}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}pub struct Santa;impl Santa { pub fn give_gift( &self, recipient: &mut impl Giftable, gift: &impl Gift, ) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(String::from("Some error").into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("gift not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::{error::Error, fmt};// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true } // Update implementation}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; } // Update implementation}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; } // Update implementation}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}pub struct Santa;impl Santa { pub fn give_gift( &self, recipient: &mut impl Giftable, gift: &impl Gift, ) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(String::from("Some error").into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid{ fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf{ fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer{ fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped (&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped (&self) -> bool{ self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self){ self.is_wrapped = true } fn is_wrapped(&self) -> bool{ self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift + fmt::Display, { if !gift.is_wrapped() { return Err(format!("Gift for {} is not wrapped!", gift, ).into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::{error::Error, fmt};// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where G: Gift, R: Giftable, { if !gift.is_wrapped() { return Err("gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, U>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> where T: Giftable, U: Gift, { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err(Box::from("BAD!")) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt; use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid{ fn receive_gift(&mut self){ self.gifted = true; }}impl Giftable for Reindeer{ fn receive_gift(&mut self){ self.gifted = true; }}impl Giftable for Elf{ fn receive_gift(&mut self){ self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool{ self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool{ self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool{ self.is_wrapped } // Update implementation}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: & impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => Err("Error".into()), }}}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, U>( &self, recipient: &mut T, gift: &U, ) -> Result<(), Box<dyn std::error::Error>> where T: Giftable, U: Gift { // 5. Update the function signature to accept any type of recipient and gift match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => Err("Error".into()), } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift (&mut self); // 1. Define the trait definition // Add a function named `receive_gift`}impl Giftable for Kid { fn receive_gift (&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift (&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift (&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped (&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped (&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped (&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped (&self) -> bool { self.is_wrapped } // Update implementation}pub struct Santa;impl Santa { pub fn give_gift <Gb, Gf> (&self, recipient: &mut Gb, gift: &Gf) -> Result<(), Box<dyn Error>> where Gb: Giftable, Gf: Gift { if !gift.is_wrapped() { return Err(String::from("Gift is not wrapped").into()) } recipient.receive_gift(); Ok(()) // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Santa;impl Santa { // pub fn give_gift(&self, recipient: Unknown, gift: Unknown) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Gift is not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error; // For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> { if ! gift.is_wrapped() { Err("Gift is not wrapped".into()) } else { Ok(recipient.receive_gift()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: & impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); return Ok(()); } Err(Box::from("not wrapped")) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: fmt::Display + Gift>( &self, recipient: &mut R, gift: &G, ) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { recipient.receive_gift(); return Ok(()); } else { return Err("Present is not wrapped")?; } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift // 1. check wrapped? if not => Err // 2. give -> recipient.gifted = true if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;pub trait Giftable { fn receive_gift(&mut self);}pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: Gift + fmt::Display>( &self, recipient: &mut R, gift: &G, ) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Nein".into()) } }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::{error::Error, fmt::{self, Display}};// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid{ fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf{ fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer{ fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool ;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Gift>(&self, recipient: &mut impl Giftable, gift: &T)-> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped(){ return Err("Gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, U>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> where T: Giftable, U: Gift, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift not prepare.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped; }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut dyn Giftable, gift: &dyn Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped!".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self); // 1. Define the trait definition // Add a function named `receive_gift`}impl Giftable for Kid { fn receive_gift(&mut self){ self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self){ self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self){ self.gifted = true }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self)->bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self)->bool{ self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self)->bool{ self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self)->bool{ self.is_wrapped } // Update implementation}pub struct Santa;impl Santa { pub fn give_gift<R,G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { if !gift.is_wrapped() { return Err("gift is not wrapped".into()) } else { recipient.receive_gift(); Ok(()) } // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, U>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> where T: Giftable, U: Gift, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("No wrap".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool { return false; } // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("The gift is not wrapped".into()) } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); return Ok(()) } return Err("The gift was already unwrapped".into()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T1, T2>(&self, recipient: &mut T1, gift: &T2) -> Result<(), Box<dyn Error>> where T1: Giftable, T2: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self, _: &impl Gift);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(Box::from("gift not wrapped")); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self, _: &impl Gift);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(Box::from("gift not wrapped")); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn std::error::Error>> { if !gift.is_wrapped() { Err("Not wrapped".into()) } else { recipient.receive_gift(); Ok(()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::{error::Error, fmt};pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self); // 1. Define the trait definition // Add a function named `receive_gift`}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Giftable, U: Gift>( &self, recipient: &mut T, gift: &U, ) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("The gift is not wrapped!".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Gift is not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { match gift.is_wrapped() { true => {recipient.receive_gift(); Ok(()) }, _ => Err("Gift is not wrapped!".into()) } // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Giftable, U: Gift>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { return Err("Gift is not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<G:Giftable, H:Gift>(&self, recipient: &mut G, gift: &H) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Gift is not wrapped!".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.#[derive(Debug)]enum NotWrappedError{ Error,}impl fmt::Display for NotWrappedError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Gift is not wrapped") }}impl Error for NotWrappedError {}pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, B>(&self, recipient: &mut T, gift: &B) -> Result<(), Box<dyn Error>> where T: Giftable, B: Gift, { if gift.is_wrapped() { Ok(recipient.receive_gift()) } else { Err(Box::new(NotWrappedError::Error)) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift;}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Elf { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<A, B>(&self, recipient: &mut A, gift: &B) -> Result<(), Box<dyn Error>> where A: Giftable, B: Gift { if !gift.is_wrapped() { return Err("not wrapped".into()); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift;}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Elf { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<A, B>(&self, recipient: &mut A, gift: &B) -> Result<(), Box<dyn Error>> where A: Giftable, B: Gift { if !gift.is_wrapped() { return Err("not wrapped".into()); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}