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.
Ferdinanddb
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("The gift is not wrapped".into()) } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
A1-exe
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); return Ok(()) } return Err("The gift was already unwrapped".into()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
FlorianGD
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { return self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T1, T2>(&self, recipient: &mut T1, gift: &T2) -> Result<(), Box<dyn Error>> where T1: Giftable, T2: Gift { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
mei28
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
sweet2honey
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self, _: &impl Gift);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(Box::from("gift not wrapped")); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
josephcopenhaver
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self, _: &impl Gift) { self.gifted = true }}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self, _: &impl Gift);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err(Box::from("gift not wrapped")); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
kapaseker
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
Sympatron
use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn std::error::Error>> { if !gift.is_wrapped() { Err("Not wrapped".into()) } else { recipient.receive_gift(); Ok(()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
MGehrmann
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
Ljungg
use std::{error::Error, fmt};pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self); // 1. Define the trait definition // Add a function named `receive_gift`}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // Update implementation}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Giftable, U: Gift>( &self, recipient: &mut T, gift: &U, ) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("The gift is not wrapped!".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
KushnerykPavel
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
sarub0b0
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift { if !gift.is_wrapped() { return Err("Gift is not wrapped.".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
rjensen
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Gift is not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
thescooby
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { match gift.is_wrapped() { true => {recipient.receive_gift(); Ok(()) }, _ => Err("Gift is not wrapped!".into()) } // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
habu1010
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T: Giftable, U: Gift>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { return Err("Gift is not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
CianciuStyles
use std::error::Error;use std::fmt;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<G:Giftable, H:Gift>(&self, recipient: &mut G, gift: &H) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("Gift is not wrapped!".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
eguefif
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.#[derive(Debug)]enum NotWrappedError{ Error,}impl fmt::Display for NotWrappedError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Gift is not wrapped") }}impl Error for NotWrappedError {}pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<T, B>(&self, recipient: &mut T, gift: &B) -> Result<(), Box<dyn Error>> where T: Giftable, B: Gift, { if gift.is_wrapped() { Ok(recipient.receive_gift()) } else { Err(Box::new(NotWrappedError::Error)) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
Ankit8848
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift;}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Elf { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<A, B>(&self, recipient: &mut A, gift: &B) -> Result<(), Box<dyn Error>> where A: Giftable, B: Gift { if !gift.is_wrapped() { return Err("not wrapped".into()); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}
wendko
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift;}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Elf { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift<G>(&mut self, _gift: &G) where G: Gift { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<A, B>(&self, recipient: &mut A, gift: &B) -> Result<(), Box<dyn Error>> where A: Giftable, B: Gift { if !gift.is_wrapped() { return Err("not wrapped".into()); } recipient.receive_gift(gift); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}
rony0000013
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 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 } // 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, R>(&self, recipient: &mut T, gift: &R) -> Result<(), Box<dyn Error>> where T: Giftable, R: 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); }}
arm01846
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, R>(&self, recipient: &mut T, gift: &R) -> Result<(), Box<dyn Error>> where T: Giftable, R: 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); }}
pagyeman
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 == true }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped == true }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped == true }}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(); Ok(()) } else { Err("Gift is not wrapped".into()) } // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
wamimi
use std::error::Error;use std::fmt;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}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 Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}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 Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift + fmt::Display, { if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err(Box::new(std::io::Error::new( std::io::ErrorKind::Other, format!("Gift {} is not wrapped!", gift), ))) } }}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); }}
titoeb
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); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // 3. Define a function named `is_wrapped` that returns a boolean 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 std::error::Error>> { if !gift.is_wrapped() { return Err("Not Wrapped!".into()); } recipient.receive_gift(); // 5. Update the function signature to accept any type of recipient and 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); }}
aaron-otis
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift(&self, recipient: &mut impl Giftable, gift: & impl Gift) -> Result<(), Box<dyn Error>> { // 5. Update the function signature to accept any type of recipient and gift if gift.is_wrapped() { recipient.receive_gift(); Ok(()) } else { Err("no".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); }}
gmvar
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 match gift.is_wrapped() { false => Err("Gift is not wrapped!".into()), true => {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); }}
tamanishi
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 match gift.is_wrapped() { true => {recipient.receive_gift(); Ok(())}, false => 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 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); }}
Nismirno
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 match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err("Gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
gsspdev
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,}// 1. Define the trait definition// Add a function named `receive_gift`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; } // Update implementation fn is_wrapped(&self) -> bool { if self.is_wrapped == true { return true; } else { return 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 { return true; } else { return 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 { return true; } else { return false; } }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, T: Gift>( &self, recipient: &mut R, gift: &T, ) -> Result<(), Box<dyn std::error::Error>> { if !gift.is_wrapped() { return Err("Gift is not wrapped".into()); } recipient.receive_gift(); Ok(()) // 5. Update the function signature to accept any type of recipient and gift }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
stanisgo
use std::fmt;pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, T: Gift>( &self, recipient: &mut R, gift: &T, ) -> Result<(), Box<dyn std::error::Error>> { 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); }}
hafihaf123
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, S: Gift>(&self, recipient: &mut T, gift: &S) -> 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); }}
Burnus
use std::fmt;use std::error::Error;pub struct Kid { pub name: String, pub gifted: bool,}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Reindeer { pub name: String, pub gifted: bool,}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}pub struct Elf { pub name: String, pub gifted: bool,}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Giftable { fn receive_gift(&mut self);}pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool;}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R: Giftable, G: Gift>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> { if !gift.is_wrapped() { Err("Gift isn't 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); }}
vaclav0411
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self) {}}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // 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, E:Gift>(&self, recipient: &mut T, gift: &E) -> 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("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); }}
joanne-cmd
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: Giftable, E: Gift>(&self, recipient: &mut T, gift: &E) -> Result<(), Box<dyn std::error::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); }}
joshvon44
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<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { // 5. Update the function signature to accept any type of recipient and gift if !gift.is_wrapped() { return Err("Gift not wrapped".into()); } recipient.receive_gift(); Ok(()) }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
joshvon44
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<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(String::from("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); }}
tonisk
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, _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>> { if gift.is_wrapped() { recipient.receive_gift(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); }}
KLcpb
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid{ fn receive_gift(&mut self){ self.gifted = true; }}impl Giftable for Reindeer{ fn receive_gift(&mut self){ self.gifted = true; }}impl Giftable for Elf {fn receive_gift(&mut self){ self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } 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() == false{ 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); }}
strachan
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } // Update implementation fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, 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("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); }}
wishkus
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped } // 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() { 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); }}
jayber
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: Giftable, E: Gift>(&self, recipient: &mut T, gift: &E) -> Result<(), Box<dyn std::error::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); }}
chriswmann
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 match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => 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); }}
lulingar
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 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<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { match gift.is_wrapped() { true => Ok(recipient.receive_gift()), false => 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); }}
Stephan-Lindner
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() { 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); }}
kanakshilledar
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 { // 5. Update the function signature to accept any type of recipient and gift pub fn give_gift<T, U>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> where T: Giftable, U: Gift { match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => Err("Gift is unwraped".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); }}
GermanS
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 { // 5. Update the function signature to accept any type of recipient and gift pub fn give_gift<T, U>(&self, recipient: &mut T, gift: &U) -> Result<(), Box<dyn Error>> where T: Giftable, U: Gift { match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) }, false => Err("Gift is unwraped".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); }}
GiulianoCTRL
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(&self, recipient: &mut impl Giftable, gift: &impl Gift) -> Result<(), Box<dyn Error>> { match gift.is_wrapped() { true => { recipient.receive_gift(); Ok(()) } false => Err("Gift not wrapped".into()) } }}pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}pub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}pub fn main() { let mut kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let mut elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let mut reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; let mut alice = Kid { name: "Alice".to_string(), gifted: false, }; let mut prancer = Reindeer { name: "Prancer".to_string(), gifted: false, }; let mut bernard = Elf { name: "Buddy".to_string(), gifted: false, }; let santa = Santa; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift); if let Ok(_) = santa.give_gift(&mut alice, &kids_gift) { println!("{} received {}", alice.name, kids_gift); assert_eq!(alice.gifted, true); } else { panic!("{} should have received {}", alice.name, kids_gift); } if let Ok(_) = santa.give_gift(&mut prancer, &reindeer_gift) { println!("{} received {}", prancer.name, reindeer_gift); assert_eq!(prancer.gifted, true); } else { panic!("{} should have received {}", prancer.name, reindeer_gift); } if let Ok(_) = santa.give_gift(&mut bernard, &elves_gift) { println!("{} received {}", bernard.name, elves_gift); assert_eq!(bernard.gifted, true); } else { panic!("{} should have received {}", bernard.name, elves_gift); }}
martynovs
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { fn receive_gift(&mut self); // 1. Define the trait definition // Add a function named `receive_gift`}impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`pub trait Gift { fn wrap(&mut self); fn is_wrapped(&self) -> bool; // 3. Define a function named `is_wrapped` that returns a boolean}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}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() { 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); }}
wlabranche
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 must be 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); }}
RedNapPanda
use std::fmt;use std::error::Error;// For a better understanding of the problem, have a look at the end of the file and see the `main`// function to see how the structs are being used.pub struct Kid { pub name: String, pub gifted: bool,}pub struct Reindeer { pub name: String, pub gifted: bool,}pub struct Elf { pub name: String, pub gifted: bool,}pub trait Giftable { // 1. Define the trait definition // Add a function named `receive_gift` fn receive_gift(&mut self);}// 2. Implement `Giftable` for `Kid`, `Reindeer`, and `Elf`impl Giftable for Kid { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Reindeer { fn receive_gift(&mut self) { self.gifted = true; }}impl Giftable for Elf { fn receive_gift(&mut self) { self.gifted = true; }}pub trait Gift { fn wrap(&mut self); // 3. Define a function named `is_wrapped` that returns a boolean fn is_wrapped(&self) -> bool;}// 4. Update the `Gift` trait implementation for `KidsGift`, `ElvesGift`, and `ReindeerGift` to// include the `is_wrapped` functionimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } fn is_wrapped(&self) -> bool { self.is_wrapped }}pub struct Santa;impl Santa { pub fn give_gift<R, G>(&self, recipient: &mut R, gift: &G) -> Result<(), Box<dyn Error>> where R: Giftable, G: Gift, { if !gift.is_wrapped() { return Err("Gift must be 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); }}