The North Pole Dev room was quiet—too quiet. Santa was still away and Blitzen was still in charge for the day, the elves didn't like that, some wished Santa's unhinged management style was back.
Blitzen leaned back in his reindeer chair knowing he's the boss now, sipping his coffee. “Hey, Snowball, did you know a function in Rust can return a reference?”
Snowball was a junior developer, he didn't know anything about Rust especially references, it seemed to confuse him, he looked up from their keyboard, skeptical. “That’s absurd. Functions return values, not references. You can’t return a borrowed value, Blitzen. It’ll dangle.”
“Oh, you poor, naive elf,” Blitzen said with a smug grin. “Behold the power of lifetimes!” He started scribbling on the whiteboard.
“Okay, but why do we even need this?” Snowball asked, raising an eyebrow. “What’s the use case?”
"We need to avoid unnecessary re-allocations, Snowball. It's more efficient this way. Remember day 2 when Santa was mad at us for a simple clone on a damn String
? It wasn't even that big of a deal!"
“Fine! You're right, Santa hates clones.”
“I challenge you, Snowball. Write a function that returns the longer string without any re-allocation. Trim the strings, compare their lengths, and make sure it doesn't involve cloning or creating new allocations.”
The two bickered about ownership, lifetimes, and why Snowball wasn’t using Arch Linux for the next hour.
Now it’s your turn. Can you help Snowball write the function and put Blitzen in his place? Show that junior developers can handle lifetimes too! Try to finish the function longer_wish
.
s1
is longer than s2
, return a reference to s1
otherwise return a reference to s2
inside a Some
variant.None
.Good Luck!
Use the trim()
method to remove any white spaces from the beginning or end of the string. e.g. s1.trim()
.
Use the chars()
method to get the UTF-8 characters in the string slice. e.g. s1.chars()
.
Use the count()
method to get the number of chracters in the string slice. e.g. s1.chars().count()
.
To return a Some
variant, you can use the Some(s1)
syntax.
Pumkko
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); match s1_len.cmp(&s2_len) { std::cmp::Ordering::Equal => None, std::cmp::Ordering::Greater => Some(s1), std::cmp::Ordering::Less => Some(s2) }}
jgpaiva
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None; } if s1_len > s2_len { return Some(s1); } Some(s2)}
jobm
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1.trim()) } else if s2.trim().chars().count() > s1.trim().chars().count() { Some(s2.trim()) } else { None }}
mliertzer
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1 = s1.trim(); let s2 = s2.trim(); let s1len = s1.chars().count(); let s2len = s2.chars().count(); if s1len > s2len { Some(s1) } else if s2len > s1len { Some(s2) } else { None }}
isaaclv
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let len_s1 = s1.trim().chars().count(); let len_s2 = s2.trim().chars().count(); if len_s1 > len_s2 { return Some(s1) } else if len_s2 > len_s1 { return Some(s2) } else { return None }}
whereswald0
use std::cmp::Ordering;// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { Ordering::Less => Some(s2), Ordering::Equal => None, Ordering::Greater => Some(s1), }}
mei28
pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len > s2_len { return Some(s1); } else if s2_len > s1_len { return Some(s2); } else { return None; }}
kapaseker
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len > s2_len { return Some(s1); } else if s2_len > s1_len { return Some(s2); } else { return None; }}
pilotso11
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); match 1 { 1 if l1 == l2 => None, 1 if l1 > l2 => Some(s1), _ => Some(s2), }}
hagl
use core::cmp::Ordering;// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { Ordering::Less => Some(s2), Ordering::Greater => Some(s1), _ => None }}
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 < l2 { return Some(s2); } if l1 > l2 { return Some(s1); } None}
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 < l2 { Some(&s2) } else if l1 > l2 { Some(&s1) } else { None } }
terminox
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here if s1.trim().chars().count() > s2.trim().chars().count() { return Some(&s1.trim()); } if s1.trim().chars().count() < s2.trim().chars().count() { return Some(&s2.trim()); } return None;}
doroshtapgh
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1 = s1.trim(); let s2 = s2.trim(); let s1l = s1.chars().count(); let s2l = s2.chars().count(); if s1l == s2l { None } else if s1l > s2l { Some(&s1) } else { Some(&s2) }}
rschmertz
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_trim = s1.trim(); let s2_trim = s2.trim(); //let s1_len = s1_trim.len(); //let s2_len = s2_trim.len(); let s1_len = s1_trim.chars().count(); let s2_len = s2_trim.chars().count(); if s1_len > s2_len { Some(s1_trim) } else if s2_len > s1_len { Some(s2_trim) } else { None }}
findgriffin
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 > l2 { return Some(&s1); } else if l1 < l2 { return Some(&s2); } else { return None; }}
chokkoyamada
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1) } else if s1.trim().chars().count() == s2.trim().chars().count() { None } else { Some(s2) }}
Ankit8848
// Write a function that returns the reference to the longer string// without any new allocationsuse std::cmp::Ordering;pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let trim_s1 = s1.trim(); let trim_s2 = s2.trim(); match trim_s1.chars().count().cmp(&trim_s2.chars().count()){ Ordering::Greater => Some(&s1), Ordering::Less => Some(&s2), Ordering::Equal => None, }}
mladen5000
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_count = s1.trim().chars().count(); let s2_count = s2.trim().chars().count(); if s1_count > s2_count { return Some(s1); } else if s1_count < s2_count { return Some(s2); } else { return None; }}
habu1010
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_length = s1.trim().chars().count(); let s2_length = s2.trim().chars().count(); if s1_length > s2_length { Some(s1) } else if s1_length < s2_length { Some(s2) } else { None }}
dade6
// Write a function that returns the reference to the longer string// without any new allocationsuse std::cmp::Ordering;pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let trim1 = s1.trim(); let trim2 = s2.trim(); match trim1.chars().count().cmp(&trim2.chars().count()){ Ordering::Greater => Some(&s1), Ordering::Less => Some(&s2), Ordering::Equal => None, }}
rjensen
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None } if s1_len > s2_len { Some(s1) } else { Some(s2) }}
json-stateham
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None; } if s1_len > s2_len { Some(s1) } else { Some(s2) }}
arm01846
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1.trim()) } else if s1.trim().chars().count() == s2.trim().chars().count() { None } else { Some(s2.trim()) }}
ankeetparikh
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_sz = s1.trim().chars().count(); let s2_sz = s2.trim().chars().count(); if s1_sz > s2_sz { Some(s1) } else if s1_sz < s2_sz { Some(s2) } else { None }}
Tarokc
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here if s1.trim().chars().count() == s2.trim().chars().count() { return None } if s1.trim().chars().count() > s2.trim().chars().count() { return Some(s1) } else { return Some(s2) }}
eguefif
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() == s2.trim().chars().count() { return None } if s1.trim().chars().count() > s2.trim().chars().count() { return Some(s1.trim()); } else { return Some(s2.trim()) }}
Nismirno
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_trimmed = s1.trim(); let s2_trimmed = s2.trim(); if s1_trimmed.chars().count() > s2_trimmed.chars().count() { return Some(s1); } else if s1_trimmed.chars().count() < s2_trimmed.chars().count() { return Some(s2); } else { return None; }}
uRTLy
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { println!(" {} {} ", s1, s2); let s1_count = s1.trim().chars().count(); let s2_count = s2.trim().chars().count(); if s1_count == s2_count { return None; } let longer = if s1_count > s2_count { s1 } else { s2 }; Some(longer)}
uRTLy
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { println!(" {} {} ", s1, s2); let s1_count = s1.trim().chars().count(); let s2_count = s2.trim().chars().count(); if s1_count == s2_count { return None; } let longer = if s1_count > s2_count { s1 } else { s2 }; Some(longer)}
antenaina0
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 > l2 { return Some(&s1); } if l1 == l2 { return None; } return Some(&s2);}
butteredtoast
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len > s2_len { return Some(s1); } else if s2_len > s1_len { return Some(s2); } None}
ctkalle
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() > s2.trim().chars().count() { return Some(s1); } else if s2.trim().chars().count() > s1.trim().chars().count() { return Some(s2); } None}
felipesaruhashi
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { if s1.trim().chars().count() > s2.trim().chars().count() { return Some(s1); } else if s1.trim().chars().count() < s2.trim().chars().count() { return Some(s2); } else { return None; }}
MCsamurai034
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1: usize = s1.trim().chars().count(); let l2: usize = s2.trim().chars().count(); if l1 > l2 { return Some(s1.trim()) } else if l2 > l1 { return Some(s2.trim()) } else { None }}
tamanishi
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1) } else if s2.trim().chars().count() > s1.trim().chars().count() { Some(s2) } else { None }}
hafihaf123
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len > s2_len { Some(&s1) } else if s1_len < s2_len { Some(&s2) } else { None }}
franlopezm
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None; } if s1_len > s2_len { Some(&s1) } else { Some(&s2) }}
franlopezm
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None; } if s1_len > s2_len { Some(s1.trim()) } else { Some(s2.trim()) }}
rirze
use std::cmp::Ordering;// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { Ordering::Greater => Some(&s1), Ordering::Less => Some(&s2), Ordering::Equal => None, } }
tdtrung17693
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len { return None; } if s1_len > s2_len { return Some(s1.trim()); } Some(s2.trim())}
KLcpb
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let i = s1.trim().chars().count() as i16 - s2.trim().chars().count() as i16; if i > 0 { Some(s1) } else if i < 0 { Some(s2) } else { None }}
BetkeJ
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here // i > 0 => s1 > s2 let i = s1.trim().chars().count() as i16 - s2.trim().chars().count() as i16; if i > 0 { Some(s1) } else if i < 0 { Some(s2) } else { None }}
leenalmajz
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let l1 = s1.trim().chars().count() as i64; let l2 = s2.trim().chars().count() as i64; if l1 > l2 { return Some(s1) } else if l2 > l1 { return Some(s2) } else { return None }}
iamsahu
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1 = s1.trim().chars().count() as i64; let l2 = s2.trim().chars().count() as i64; match l1 - l2 { 0 => None, 1.. => Some(s1), _ => Some(s2) }}
phate45
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let left = s1.trim(); let right = s2.trim(); if left.len() == right.len() { return None; } if left.chars().count() > right.chars().count() { return Some(left); } return Some(right);}
sarub0b0
// Write a function that returns the reference to the longer string// without any new allocationsuse std::cmp::Ordering;pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1 = s1.trim(); let s2 = s2.trim(); match s1.chars().count().cmp(&s2.chars().count()) { Ordering::Less => Some(s2), Ordering::Equal => None, Ordering::Greater => Some(s1) }}
arilet
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); // Your code here if l1 > l2 { return Some(&s1); } if l2 > l1 { return Some(&s2); } None}
OwOday
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let s1_cool = s1.trim().chars().count(); let s2_cool = s2.trim().chars().count(); if s1_cool == s2_cool{ return None; } if s1_cool > s2_cool { Some(s1.trim()) } else{ Some(s2.trim()) }}
gmvar
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // Your code here let string1 = s1.trim(); let string2 = s2.trim(); if string1.chars().count() == string2.chars().count() { return None; } if string1.chars().count() > string2.chars().count() { return Some(string1); } return Some(string2);}