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 tried their best, here is what they've written so far:
However, the code won't compile. Can you help the elves attach the message to the present and print the message too?
Maybe... if there was only a way to get a clone of the message and pass it to the function. 🤔
attach_message_to_present
function.clone()
to create a clone of the gift_message
and pass it to the function.sukman
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);}
Carreau
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);}
admiraladmirable
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);}
Soecka
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);}
SchnobiTobi
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);}
ohorn
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);}
markuswege
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);}
c4s4
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);}
Ruspoetin
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);}
recursivemaze
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);}
brunoalef7
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);}
LenRemmerswaal
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);}
gkapkowski-saucelabs
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);}
doeixd
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);}
dasunpubudumal
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);}
robotomize
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);}
midnightexigent
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);}
rawar089
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);}
zealsham
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);}
sgt
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);}
fletawe-haulogy
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);}
dalprahcd
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);}
mrsalo
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);}
WuHan0608
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);}
neonwarp
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);}
aockgonnabefree
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);}
LaserPH
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);}
theandi667
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);}
var4yn
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);}
jeffglaum
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);}
jeffglaum
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);}
rubonz
use std::ops::Deref;const SNOWBALL_WEIGHT_KG: f64 = 0.2;const SNOWBALL_WEIGHT_LB: f64 = 0.441;pub struct SnowKg(pub f64);impl SnowKg { pub fn new(kg: f64) -> Self { SnowKg(kg) }}impl Deref for SnowKg { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}pub struct SnowLb(pub f64);impl SnowLb { pub fn new(lb: f64) -> Self { SnowLb(lb) }}impl Deref for SnowLb { type Target = f64; fn deref(&self) -> &Self::Target { &self.0 }}pub struct Snowball(pub i64);impl Snowball { pub fn new(snowballs: i64) -> Self { Snowball(snowballs) }}impl Deref for Snowball { type Target = i64; fn deref(&self) -> &Self::Target { &self.0 }}impl From<SnowKg> for Snowball { fn from(kg: SnowKg) -> Self { let snowballs = (*kg / SNOWBALL_WEIGHT_KG).round() as i64; Snowball(snowballs) }}impl From<SnowLb> for Snowball { fn from(lb: SnowLb) -> Self { let snowballs = (*lb / SNOWBALL_WEIGHT_LB).round() as i64; Snowball(snowballs) }}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);}
0xkuznechik
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);}
kj-9
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);}
jon-a-miller
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);}
nikwi
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);}
nikwi
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);}
plauche
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);}
dpathakj
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);}
Kada-quantum
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);}
Shaphil
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);}
vabka
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);}
jpekmez
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);}
IvanyaK
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);}
mbiette
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);}
edgarcnp
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);}
yuankunzhang
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);}
tuusberg
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);}
maplestarplayl
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);}
rostyq
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);}