We use clone()
in this example, but we are not claiming it is the optimal approach. The goal here is to present a solution that helps illustrate Rust's ownership system in a simple way. As the story unfolds in the next challenge, we'll dive into more efficient patterns. This example is intentionally simplified for the sake of education and storytelling.
Enjoy!
It's 1st December and the countdown has just begun. The elves are busy preparing for the Christmas and Santa is busy checking the list of children who have been good this year. It was supposed to be a smooth day until all of a sudden two of santa's elves burst into Santa's office with a problem.
“Santa!” one of the elves shouted. “The code won’t compile! We’ve hit a wall, and it’s all Rust’s fault!”
Santa, sipping his triple-shot peppermint latte, raised an eyebrow. “Rust’s fault? Or your fault?”
“It’s the ownership rules!” the other elve blurted. “I think we violated them, we’re used to Python, where variables just... work. Look at this!”
The elves have tried their best but the code doesn't compile.
You need to find a way to make the code compile without changing the attach_message_to_present
function.
clone()
to create a clone of the gift_message
and pass it to the function.tdoan
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
dnkolegov
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
jgpaiva
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
EphraimBsB
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
s-umn04
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
anjiava
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
henopied
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
henopied
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
lianweiting1995
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
mmgajda
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
mei28
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
jiztastamablastamarang
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
sergey-sonkin
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Ducky2048
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
zcfxx
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
mliertzer
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
doroshtapgh
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Deaddy3
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
chenyaoy
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
zahrajavar
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
pgray
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
klllr
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
chokkoyamada
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
chokkoyamada
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
pilotso11
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
findgriffin
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
pwhenryuk
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Sanket6652
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
habu1010
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
rjensen
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Ankit8848
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Pumkko
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
rodynenko
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Flash-Kaa
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
migusmp
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
ankeetparikh
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
eguefif
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
MrDaves24
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
duuinh
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
saltheman
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
tamanishi
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
hagl
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
hafihaf123
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
LeGrandMendes
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
KLcpb
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
thebearpark
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
sreyassabbani
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
kuznetsss
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
aidan1magee
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}
Mor8
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(gift_message.clone()); // <-- Add a `.clone()` here! println!("{}", gift_message);}// Don't change this function!pub fn attach_message_to_present(message: String) { println!("The present now has this message: {}", message);}