“Bernard! Did I tell you? Trait bounds are the greatest thing since reindeer! The Display
trick you pulled yesterday? Pure genius. I’m hooked.”
Bernard shuffled in, already regretting showing Santa Display
. “What now?”
“The gifts won’t wrap themselves! I need a function—generic, reusable, beautiful. We slap on some bounds, and boom, wrapping perfection!”
Bernard peeked at the screen. “Santa, you’ve got mutable and immutable borrows fighting each other. And what’s with this raw pointer?”
Santa grinned. “Bounds solve everything, Bernard! I’ll slap a Display
here, a Debug
there—easy!”
The IDE chimed angrily. “Cannot borrow self
as mutable while also borrowed as immutable.”
Bernard sighed. “Santa, that’s the compiler telling you no.”
Santa glared at the screen. “Warm up the sled. I’m giving the borrow checker a piece of my mind.”
What you need to do is simple, Santa has written a function prepare_gift
, but it doesn't work quite yet. The function must accept anything that implements the Display
and Gift
trait. And the Gift
trait must have a method wrap
that mutates the is_wrapped
field to true
.
Here's what you gotta do:
is_wrapped: bool
.Gift
trait definition, add a method called wrap
.Gift
trait for KidsGift
, ElvesGift
, and ReindeerGift
. Using the wrap
method, it should set is_wrapped
to true
.prepare_gift
function signature to accept any type that implements the Display
and Gift
traits.If you're stuck or need a starting point, here are some hints to help you along the way!
The Gift
trait should have a method wrap
that takes a mutable reference of self
. e.g.
pub trait Gift {
fn wrap(&mut self);
}
To make a function accept any type that implements multiple traits, you can use the +
operator. e.g.
fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) {
// code remains the same
}
Make sure the argument takes a mutable reference &mut T
to be able to call the wrap
method since wrap
also mutates the struct.
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// Subtrait pattern - everything that implements Gift must also implement Displaypub trait Gift: fmt::Display { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: Gift>(gift: &mut T) { println!("Preparing gift for {}", gift); gift.wrap(); println!("Gift wrapped for {}", gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { fn wrap(&mut self);} // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);} // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift // 2. Finish the trait definition //{ fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { // 2. Finish the trait definition // fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped : bool,}pub struct ElvesGift { pub name: String, pub is_wrapped : bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped : bool,}//2. Implement the Gift Trait definition and implementation on structspub trait Gift { fn wrap(&mut self);}// 3. Update the function signature - Need Mutable Access !pub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift{ fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift{ fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift{ fn wrap(&mut self) { self.is_wrapped = true }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { // 2. Finish the trait definition // fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(& mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;pub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; println!("Wrapping kids gift: {}", self.name); }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; println!("Wrapping elves gift: {}", self.name); }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; println!("Wrapping reindeer gift: {}", 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);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift // 2. Finish the trait definition //{ fn wrap(&mut self) -> (); }// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift{ // 2. Finish the trait definition // fn wrap(&mut self)->();}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool }pub trait Gift { fn is_wrapped_mut(&mut self) -> &mut bool; //generic implementation fn wrap(&mut self) -> () { *self.is_wrapped_mut() = true; //not &self, not Self. need to deref and call the function //to expose the trait. }} // 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { //the syntax is gift: &mut T not &mut gift: T // println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn is_wrapped_mut(&mut self) -> &mut bool { &mut self.is_wrapped }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn is_wrapped_mut(&mut self) -> &mut bool { &mut self.is_wrapped }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn is_wrapped_mut(&mut self) -> &mut bool { &mut self.is_wrapped }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self){ self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;pub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display>(gift: &mut T) whereT: Gift{ println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// pub fn prepare_gift<T: fmt::Display>(gift: T) {// println!("Preparing gift for {}", &gift);// gift.wrap();// println!("Gift wrapped for {}", &gift);// }pub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self){ self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self){ self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { fn wrap(&mut self) {} } // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; } }impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; } }impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; } }impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped : bool,}pub struct ElvesGift { pub name: String, pub is_wrapped : bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped : bool,}pub trait Gift { // 2. Finish the trait definition // fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift // 2. Finish the trait definition //{ fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);} // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}impl Gift for &mut KidsGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for &mut ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for &mut ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for KidsGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift // 2. Finish the trait definition // { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift{ fn wrap(&mut self);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;pub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T>(gift: &mut T) where T: Gift + fmt::Display{ println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub is_wrapped: bool, pub name: String,}pub struct ElvesGift { pub is_wrapped: bool, pub name: String,}pub struct ReindeerGift { pub is_wrapped: bool, pub name: String,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: Gift + fmt::Display>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}pub trait Gift {// 2. Finish the trait definition // fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift:&mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { // 2. Finish the trait definition // fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);} // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);} // 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap (&mut self);}impl Gift for KidsGift { fn wrap (&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap (&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap (&mut self){ self.is_wrapped = true; }}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}// 2. Finish the trait definition // pub trait Gift { fn wrap(&mut self);} // 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = !self.is_wrapped }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = !self.is_wrapped }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = !self.is_wrapped }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structs#[derive(Debug)]pub struct KidsGift { pub name: String, pub is_wrapped: bool,}#[derive(Debug)]pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}#[derive(Debug)]pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: Gift + std::fmt::Display>(mut gift: T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift{ fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl Gift for ElvesGift{ fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl Gift for ReindeerGift{ fn wrap(&mut self) -> () { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(kids_gift); prepare_gift(elves_gift); prepare_gift(reindeer_gift);}
use std::fmt::{Debug, Display, Formatter, Result};// 1. Add the `is_wrapped` field to the gift structs#[derive(Debug)]pub struct KidsGift { pub name: String, pub is_wrapped: bool,}#[derive(Debug)]pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}#[derive(Debug)]pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}// 2. Finish the trait definition //pub trait Gift { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift<T: Gift + Display + Debug>(mut gift: T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true }}impl Display for KidsGift { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}", self.name) }}impl Display for ElvesGift { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}", self.name) }}impl Display for ReindeerGift { fn fmt(&self, f: &mut Formatter) -> Result { write!(f, "{}", self.name) }}pub fn main() { let kids_gift = KidsGift { name: "toy car".to_string(), is_wrapped: false, }; let elves_gift = ElvesGift { name: "vertical monitor".to_string(), is_wrapped: false, }; let reindeer_gift = ReindeerGift { name: "carrot".to_string(), is_wrapped: false, }; prepare_gift(kids_gift); prepare_gift(elves_gift); prepare_gift(reindeer_gift);}
use std::fmt;use std::fmt::Display;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool}pub struct ElvesGift { pub name: String, pub is_wrapped: bool}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool}// 2. Finish the trait definition //pub trait Gift: Display { fn wrap(&mut self);}// 3. Update the function signaturepub fn prepare_gift(gift: &mut impl Gift) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}
use std::fmt;// 1. Add the `is_wrapped` field to the gift structspub struct KidsGift { pub name: String, pub is_wrapped: bool,}pub struct ElvesGift { pub name: String, pub is_wrapped: bool,}pub struct ReindeerGift { pub name: String, pub is_wrapped: bool,}pub trait Gift { fn wrap(&mut self);}// 2. Finish the trait definition //// 3. Update the function signaturepub fn prepare_gift<T: fmt::Display + Gift>(gift: &mut T) { println!("Preparing gift for {}", &gift); gift.wrap(); println!("Gift wrapped for {}", &gift);}// 4. Implement the Gift trait to the gift structsimpl fmt::Display for KidsGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for KidsGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ElvesGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ElvesGift { fn wrap(&mut self) { self.is_wrapped = true; }}impl fmt::Display for ReindeerGift { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) }}impl Gift for ReindeerGift { fn wrap(&mut self) { self.is_wrapped = true; }}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, }; prepare_gift(&mut kids_gift); prepare_gift(&mut elves_gift); prepare_gift(&mut reindeer_gift);}