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
}
}
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);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { 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 let res = match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Something went wrong")) as Box<dyn Error>) }; return res; }}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);}// 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() { return Err("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(&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::{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, gift: &impl Gift);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self, _gift: &impl Gift) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self, _gift: &impl Gift) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self, _gift: &impl Gift) { 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 } // 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("gift 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);}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);}// 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 match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err("Failed to wrap a gift".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); // 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;use std::error::Error;impl Santa { pub fn give_gift<T1: Giftable, T2: Gift>(&self, recipient: &mut T1, gift: &T2) -> Result<(), Box<dyn Error>> { if !gift.is_wrapped() { return Err("Gift 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;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 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;}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: Giftable, Q: Gift>(&self, recipient: &mut T, gift: &Q) -> Result<(), Box<dyn Error>> { 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); 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 { // 5. Update the function signature to accept any type of recipient and gift pub fn give_gift<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> { if !gift.is_wrapped() { Err("The 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::{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);}// 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 { // 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() { 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 santa.give_gift(&mut alice, &kids_gift).is_ok() { println!("{} received {}", alice.name, kids_gift); assert!(alice.gifted); } else { panic!("{} should have received {}", alice.name, kids_gift); } if santa.give_gift(&mut prancer, &reindeer_gift).is_ok() { println!("{} received {}", prancer.name, reindeer_gift); assert!(prancer.gifted); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if santa.give_gift(&mut bernard, &elves_gift).is_ok() { println!("{} received {}", bernard.name, elves_gift); assert!(bernard.gifted); } 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<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::{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 { 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( &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 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, 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 fn receive_gift(&mut self);}macro_rules! impl_giftable_trait { ($($name: ident), +) => { $( impl Giftable for $name { fn receive_gift(&mut self) { self.gifted = true; } } )* };}impl_giftable_trait!(Kid, Reindeer, Elf);// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}macro_rules! impl_gift_trait { ($($name: ident), +) => { $( impl Gift for $name { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } } )* };}impl_gift_trait!(KidsGift, ElvesGift, ReindeerGift);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("???".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(&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 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;// 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<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn std::error::Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { Err("unwrapped".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::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 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;}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 } // 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>> { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("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 of {}", &gift); gift.wrap(); println!("Gift wrapped {}", &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<A: Giftable, B: Gift>(&self, recipient: &mut A, gift: &B) -> 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: 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() { let _ = Err("Not wrapped")?; } 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);}macro_rules! imp_giftable { ($($t:ty), +) => { $( impl Giftable for $t { fn receive_gift(&mut self) { self.gifted = true }})+ };}imp_giftable!(Kid, Reindeer, Elf);pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}macro_rules! imp_gift { ($($t:ty), +) => { $( impl Gift for $t { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }})+ };}imp_gift!(KidsGift, ReindeerGift, ElvesGift);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("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);}// 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; } 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("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 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` functionmacro_rules! gift_type { ($($gift:ident),+) => { $( impl Gift for $gift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } } )*};}gift_type!(ElvesGift, ReindeerGift, KidsGift);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() { Err("unwrapped".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 } // 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<T : Giftable, G: Gift>(&self, recipient: &mut T, gift: &G) -> Result<(), Box<dyn Error>> { // 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::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; } 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>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("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, gift: &impl Gift) -> Result<(), Box<dyn Error>>;}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self, gift: &impl Gift) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { self.gifted = true; Ok(()) } else { Err("Gift is not wrapped".into()) } }}impl Giftable for Elf { fn receive_gift(&mut self, gift: &impl Gift) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { self.gifted = true; Ok(()) } else { Err("Gift is not wrapped".into()) } }}impl Giftable for Reindeer { fn receive_gift(&mut self, gift: &impl Gift) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { self.gifted = true; Ok(()) } else { Err("Gift is not wrapped".into()) } }}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; } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped; } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return 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>> { recipient.receive_gift(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::{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 { fn receive_gift(&mut self, gift: &impl Gift) -> Result<(), Box<dyn Error>>; // 1. Define the trait definition // Add a function named `receive_gift`}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`macro_rules! impl_giftable { ($struct:ty) => { impl Giftable for $struct { fn receive_gift(&mut self, gift: &impl Gift) -> Result<(), Box<dyn Error>> { if gift.is_wrapped() { self.gifted = true; Ok(()) } else { Err("Gift is not wrapped".into()) } } } };}impl_giftable!(Kid);impl_giftable!(Reindeer);impl_giftable!(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` functionmacro_rules! impl_gift { ($struct:ty) => { impl Gift for $struct { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } } };}impl_gift!(KidsGift);impl_gift!(ElvesGift);impl_gift!(ReindeerGift);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, { recipient.receive_gift(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("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);}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 Error>> { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Unable to give gift as it isn't 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 { // 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 } // 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 std::error::Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("no 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 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 match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err(Box::from("Gift isn't 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.#[derive(Debug)]pub struct UnwrappedError {}impl fmt::Display for UnwrappedError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Gift isn't wrapped") }}impl Error for UnwrappedError {}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 match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err(Box::new(UnwrappedError {})), } }}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: 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() { return Err(Box::from("gift is not wrapped")); } 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 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, { if !gift.is_wrapped() { return Err("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;// 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; } 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<TGiftable: Giftable, TGift: Gift>( &self, recipient: &mut TGiftable, gift: &TGift, ) -> Result<(), Box<dyn std::error::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); 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, { // 5. Update the function signature to accept any type of recipient and gift 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 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(&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::{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);}// 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 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 santa.give_gift(&mut alice, &kids_gift).is_ok() { println!("{} received {}", alice.name, kids_gift); assert!(alice.gifted); } else { panic!("{} should have received {}", alice.name, kids_gift); } if santa.give_gift(&mut prancer, &reindeer_gift).is_ok() { println!("{} received {}", prancer.name, reindeer_gift); assert!(prancer.gifted); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if santa.give_gift(&mut bernard, &elves_gift).is_ok() { println!("{} received {}", bernard.name, elves_gift); assert!(bernard.gifted); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}#[cfg(test)]mod test { use crate::main; #[test] fn test_main() { main(); }}
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 { 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, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } 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::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`- DONEimpl 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 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() { recipient.receive_gift(); Ok(()) } else { Err("Gift not wrapped".into()) } }}// ========================================================================== //pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean - DONE 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 - DONE}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation - DONE}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation - DONE}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()); } 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<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()); } 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,}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 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; } // 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 match gift.is_wrapped() { true => return Ok(recipient.receive_gift()), _ => 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::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() { 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::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: 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() { 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 { // 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("The gift is not wraped".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 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("The gift is not wraped".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::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 { 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 } // 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>> { //T is just a placeholder, you can use any constants you want! if !gift.is_wrapped() { return Err("The 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, &mut 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, &mut 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, &mut 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 Reindeer{ fn receive_gift(&mut self){ self.gifted = true; }}impl Giftable for Kid{ 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{ if self.is_wrapped{ true } else{ false } }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool{ if self.is_wrapped{ true } else{ false } }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool{ if self.is_wrapped{ true } else{ false } }}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(){ recipient.receive_gift(); Ok(()) } else{ Err(Box::from("Gift is not wraped yet")) } }}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 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 { // 3. Define a function named `is_wrapped` that returns a boolean 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, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if gift.is_wrapped() { recipient.receive_gift(); return Ok(()); } Err(Box::from("Gift 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 { 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: Giftable, U: Gift>(&self, recipient: &mut T, gift: & U) -> Result<(), Box<dyn Error>> { if !gift.is_wrapped() { return Err(Box::from("Gift is not wrapped?!")); } 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::{self}};// 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;}// 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() { Err(Box::from("The gift is not wrapped.")) } 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); }}