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.
// 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 match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { Ordering::Equal => None, Ordering::Greater => Some(s1), _ => Some(s2), }}
// 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.chars().count(); let s2_len = s2_trim.chars().count(); if s1_len > s2_len { Some(s1_trim) } else if s1_len < s2_len { Some(s2_trim) } else { 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> { if s1.trim().chars().count() == s2.trim().chars().count(){ None }else if s1.trim().chars().count() >= s2.trim().chars().count(){ Some(&s1) }else{ Some(&s2) }}
// 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 println!("{}",s1); println!("{}",s2); if s1.trim().chars().count() == s2.trim().chars().count() { None } else if s1.trim().chars().count() < s2.trim().chars().count() { Some(s2) } else { Some(s1) } }
// 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 use std::cmp::Ordering; let s1 = s1.trim(); let s2 = s2.trim(); match s1.chars().count().cmp(&s2.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> { // Your code here let s1_trimmed = s1.trim(); let s2_trimmed = s2.trim(); let len1 = s1_trimmed.chars().count(); let len2 = s2_trimmed.chars().count(); match len1.cmp(&len2){ std::cmp::Ordering::Greater => Some(s1_trimmed), std::cmp::Ordering::Less => Some(s2_trimmed), std::cmp::Ordering::Equal => 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 s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); println!("{}, {}", s1_len, s2_len); if s1_len > s2_len { return Some(s1) } else if s1_len == s2_len { return None } else { return Some(s2) }}
// 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(){ None }else if s1.trim().chars().count() > s2.trim().chars().count(){ Some(s1) }else{ Some(s2) }}
// 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 }}
use std::cmp::Ordering;pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { Ordering::Equal => None, Ordering::Greater => Some(s1.trim()), Ordering::Less => Some(s2.trim()), }}
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::Equal => None, Ordering::Greater => Some(s1), Ordering::Less => Some(s2) }}
pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { std::cmp::Ordering::Equal => None, std::cmp::Ordering::Greater => Some(s1), std::cmp::Ordering::Less => Some(s2), }}
// Write a function that returns the reference to the longer string// without any new allocations// If s1 is longer than s2, return a reference to s1 otherwise return a reference to s2 inside a Some variant.// If both slices have the same length, return None.// Any white spaces in the beginning or end of the string should be trimmed.pub 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 s1_len = s1.chars().count(); let s2_len = s2.chars().count(); match s1_len.cmp(&s2_len) { std::cmp::Ordering::Equal => { // Code for when s1.len() == s2.len() return None; }, std::cmp::Ordering::Greater => { // Code for when s1.len() > s2.len() return Some(s1); }, std::cmp::Ordering::Less => { // Code for when s1.len() < s2.len() return Some(s2); }, }}
// 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> { // Trim both strings before comparing their lengths let s1_trimmed = s1.trim(); let s2_trimmed = s2.trim(); // Return None if both strings are empty after trimming if s1_trimmed.is_empty() && s2_trimmed.is_empty() { return None; } if s1_trimmed.len() == s2_trimmed.len() { return None; } // Return the longer string or the first one if they're of equal length if s1_trimmed.chars().count() > s2_trimmed.chars().count() { // if s1_trimmed.parse::<u32>().is_ok() { return None} Some(s1_trimmed) } else { // if s2_trimmed.parse::<u32>().is_ok() { return None} Some(s2_trimmed) }}
// 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 { None } else if s1_length > s2_length { Some(s1) } else { Some(s2) }}
// 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_trimed = s1.trim(); let s2_trimed = s2.trim(); let s1_length = s1_trimed.chars().count(); let s2_length = s2_trimed.chars().count(); if s1_length > s2_length { return Some(s1) } else if s2_length > s1_length { return Some(s2) } 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> { // Your code here let len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 == len2 { return None; } if len1 < len2 { return Some(&s2); } else { return Some(&s1); };}
// 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 usize::cmp(&s1.trim().chars().count(), &s2.trim().chars().count()) { std::cmp::Ordering::Greater => Some(s1), std::cmp::Ordering::Less => Some(s2), _ => None, }}
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 let len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); match len1.cmp(&len2) { Ordering::Greater => Some(s1), Ordering::Less => Some(s2), Ordering::Equal => 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 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) } else { return Some(s2) }}
// 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_t = s1.trim().chars().count(); let s2_t = s2.trim().chars().count(); if s1_t > s2_t { Some(s1) } else if s2_t > s1_t { Some(s2) } else { None }}
use std::cmp::Ordering;pub 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(); match s1_count.cmp(&s2_count) { Ordering::Equal => None, Ordering::Greater => Some(s1), Ordering::Less => Some(s2), }}
// 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(); println!("s1: {}, len: {}", s1, s1_len); println!("s2: {}, len: {}", s2, s2_len); if s1_len > s2_len { return Some(s1); } else if s1_len < s2_len { return Some(s2); } None}
use std::cmp::Ordering;pub 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(); match s1_count.cmp(&s2_count) { Ordering::Equal => None, Ordering::Greater => Some(s1), Ordering::Less => Some(s2), }}
// 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_lenght = s1.trim().chars().count(); let s2_lenght = s2.trim().chars().count(); return match s1_lenght >= s2_lenght { true if s1_lenght == s2_lenght => None, true => Some(s1), false => Some(s2),}}
// 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 { None } else if s1_len > s2_len { Some(s1) } else { Some(s2) }}
// 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 s1t = s1.trim(); let s2t = s2.trim(); if s1t.len() == s2t.len() { None } else if s1t.chars().count() > s2t.chars().count() { Some(s1) } else { Some(s2) }}
// 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_trimmed = s1.trim(); let s2_trimmed = s2.trim(); let s1_char_count = s1_trimmed.chars().count(); let s2_char_count = s2_trimmed.chars().count(); if s1_char_count == s2_char_count { return None } if s1_char_count > s2_char_count { return Some(s1_trimmed) } else { return Some(s2_trimmed) }}
// 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, s2)=(s1.trim_start().trim_end(),s2.trim_start().trim_end()); if s1.chars().count() == s2.chars().count() { return None; } if s1.chars().count() > s2.chars().count() { return Some(s1); } if s1.chars().count() < s2.chars().count() { return Some(s2); } 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 s1_trimmed = s1.trim(); let s2_trimmed = s2.trim(); let s1_char_count = s1_trimmed.chars().count(); let s2_char_count = s2_trimmed.chars().count(); if s1_char_count > s2_char_count { return Some(s1_trimmed); } else if s1_char_count < s2_char_count { return Some(s2_trimmed); } else { return 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> { 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 }}
// 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 size_s1 = s1.trim().chars().count(); let size_s2 = s2.trim().chars().count(); if size_s1 == size_s2{ return None; } else if size_s1 > size_s2{ return Some(&s1); } else{ return Some(&s2); } }
// 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 count_s1 = s1.trim().chars().count(); let count_s2 = s2.trim().chars().count(); if count_s1 == count_s2 { return None; } if count_s1 > count_s2 { Some(&s1) } else { Some(&s2) }}
// 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 { None } else if l1 > l2 { Some(&s1) } else { Some(&s2) }}
// Write a function that returns the reference to the longer string// without any new allocationsuse std::cmp::Ordering as O;pub fn longer_wish <'a> (s1: &'a str, s2: &'a str) -> Option<&'a str> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { O::Greater => Some(s1), O::Less => Some(s2), O::Equal => 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> { // Your code here if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1) } else if s1.trim().chars().count() < s2.trim().chars().count() { Some(s2) } else { 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> { // Your code here let x1 = s1.trim(); let x2 = s2.trim(); if x1.chars().count() == x2.chars().count() { return None; } else if x1.chars().count() > x2.chars().count() { return Some(s1); } else { return Some(x2); }}
// 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() { None } else if s1.chars().count() > s2.chars().count() { Some(s1.trim()) } else { Some(s2.trim()) }}
// 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 s1t = s1.trim(); let s2t = s2.trim(); let c1 = s1t.chars().count(); let c2 = s2t.chars().count(); if c1 > c2 { return Some(s1); } else if c2 > c1 { return Some(s2); } else { return 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> { // Your code here let trimmed_s1 = s1.trim(); let trimmed_s2 = s2.trim(); let len_s1 = trimmed_s1.chars().count(); let len_s2 = trimmed_s2.chars().count(); if len_s1 > len_s2 { Some(s1) } else if len_s2 > len_s1 { Some(s2) } else { 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> { // Your code here 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()) }}
// 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.trim()) } else { return Some(s2.trim()) }}
// 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, s2_len) { (l1, l2) if l1 > l2 => Some(s1), (l1, l2) if l1 < l2 => Some(s2), _ => 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> { // Your code here let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len == s2_len {return None}; Some(if s1_len > s2_len {s1} else {s2})}
// 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 = s1.trim(); let s2 = s2.trim(); if s1.chars().count() == s2.chars().count() { None } else if s1.chars().count() > s2.chars().count() { return Some(s1); } else { return Some(s2); }}
// Write a function that returns the reference to the longer string// without any new allocationspub fn longer_wish<'a>(ss1: &'a str, ss2: &'a str) -> Option<&'a str> { // Your code here let s1 = ss1.trim(); let s2 = ss2.trim(); if s1.chars().count() == s2.chars().count() { None } else if s1.chars().count() < s2.chars().count() { Some(s2) } else { Some(s1) }}
// 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 s1Count = s1.chars().count(); let s2Count = s2.chars().count(); if s1Count == s2Count { return None; } if s1Count > s2Count { return Some(s1); } Some(s2)}
// 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) }}
// 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)}
// 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 }}