We'd like to clarify our intention behind Day 1 of Advent of Rust on Rustfinity. The challenge involved cloning a string to use it twice, which some people understandably judged too quickly and labeled as "teaching bad practice." We understand why it might have been seen this way, but that was not our goal.
The decision to use cloning was intentional, meant for educational purposes and to align with the story's style. We apologize for any misunderstanding this may have caused. In today's challenge, we'll revisit and demonstrate a better approach, showcasing more optimal Rust practices.
Thank you for your patience and understanding as the story continues to unfold!
Enjoy!
Santa stormed into the workshop like a grizzly bear that just got paged for an on-call issue at 3 AM. Santa's face twisted into a mix of frustration and disbelief—a dire sign that even his usually jolly demeanor couldn't mask the disappointment.
“Who wrote this monstrosity?!” Santa boomed, holding a printed stack of code like it was his naughty list. “Do you think memory grows on Christmas trees? CLONE?! Another allocation for the same string?!”
The room fell silent. The elves exchanged nervous glances; it seemed like the code you wrote yesterday wasn't up to Santa's standards.
“But Santa,” one of the elves began, “we thought cloning was safe! No borrow-checker drama—"
“Safe? Sure. Efficient? NO!” Santa interrupted. “We need to use references! Borrow the data, don’t hog it! Everything has its own place, and using .clone()
here is not! This is Rust! Memory efficiency is the whole point!”
An elve raised a hand meekly. “But Santa, in JavaScript, you just put it in and it works!”
Santa sighed, pinching the bridge of his nose. “Listen, bros, here’s the deal: get this code refactored using references. If I see one more .clone()
without a good reason, I’m switching to Zig. Now, fix it before I start yapping on stream about incompetent elves!”
The elves exchanged panicked glances, knowing that if they messed up, Blitzen would be called in to pair-program and nobody wanted that because Blitzen wouldn't stop talking about his neovim macros.
The elves gulped. It was time to work with something that they had never seen before: borrowing and references.
Can you help them fix the code?
attach_message_to_present
function to accept a reference to a String
or a string slice str
instead of an owned String
.main
function to pass a reference to the gift_message
string instead of cloning it.sukman
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}
StephaneTrebel
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}
c4s4
pub fn main() { let gift_message = "Merry Christmas! Enjoy your gift!"; attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &String) { println!("The present now has this message: {}", message);}
alankle
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message.to_string());}
Ruspoetin
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &String) { println!("The present now has this message: {}", message);}
midnightexigent
pub fn main() { let gift_message = "Merry Christmas! Enjoy your gift!"; attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", &gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}
AviAvni
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}
jpekmez
pub fn main() { let gift_message = "Merry Christmas! Enjoy your gift!"; attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { 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); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}
creativecoder
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &String) { println!("The present now has this message: {}", message);}
filippodebortoli
pub fn main() { let gift_message = String::from("Merry Christmas! Enjoy your gift!"); attach_message_to_present(&gift_message); println!("{}", gift_message);}pub fn attach_message_to_present(message: &str) { println!("The present now has this message: {}", message);}