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::Greater => Some(s1), Ordering::Equal => None, _ => 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() { Some(s1) } else if s1.trim().chars().count() < s2.trim().chars().count() { Some(s2) } else if s1.trim().chars().count() == s2.trim().chars().count() { None } else { 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> { let s1 = s1.trim(); let s2 = s2.trim(); return match s1.chars().count().cmp(&s2.chars().count()) { Ordering::Less => Some(s2), Ordering::Greater => Some(s1), 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> { // 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> { let (s1, s2) = (s1.trim(), s2.trim()); match s1.chars().count().cmp(&s2.chars().count()) { std::cmp::Ordering::Less => Some(s2), std::cmp::Ordering::Greater => Some(s1), std::cmp::Ordering::Equal => None, }}
pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { // len() returns length in bytes, but Rust strings are UTF-8 so may contain multibyte characters. // Therefore use chars().count() instead. let str1 = s1.trim().chars().count(); let str2 = s2.trim().chars().count(); if str1 == str2 { None } else if str1 > str2 { 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 if s1.trim().chars().count() > s2.trim().chars().count() { Some(s1.trim()) } else if s1.trim().chars().count() < s2.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 s1_length = s1.trim().chars().count(); let s2_length = s2.trim().chars().count(); if s1_length > s2_length { return Some(&s1); } else if s2_length > s1_length { 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> { // len() returns length in bytes, but Rust strings are UTF-8 so may contain multibyte characters. // Therefore use chars().count() instead. let s1_len = s1.trim().chars().count(); let s2_len = s2.trim().chars().count(); if s1_len > s2_len { Some(s1) } else if s2_len > s1_len { 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 s1_size = s1.trim().chars().count(); let s2_size = s2.trim().chars().count(); if s1_size > s2_size { Some(s1) } else if s1_size < s2_size { 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> { let trimmed_s1 = s1.trim(); let trimmed_s2 = s2.trim(); match trimmed_s1.chars().count().cmp(&trimmed_s2.chars().count()) { std::cmp::Ordering::Greater => Some(trimmed_s1), std::cmp::Ordering::Less => Some(trimmed_s2), 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 trimmed_s1 = s1.trim(); let trimmed_s2 = s2.trim(); match trimmed_s1.chars().count().cmp(&trimmed_s2.chars().count()) { std::cmp::Ordering::Greater => Some(s1), std::cmp::Ordering::Less => Some(s2), 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> { match s1.trim().chars().count().cmp(&s2.trim().chars().count()) { std::cmp::Ordering::Greater => Some(s1.trim()), std::cmp::Ordering::Less => Some(s2.trim()), std::cmp::Ordering::Equal => None, }}
use std::cmp::Ordering;pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_size = s1.trim().chars().count(); let s2_size = s2.trim().chars().count(); match s1_size.cmp(&s2_size) { Ordering::Less => Some(s2), Ordering::Equal => None, Ordering::Greater => 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 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} }
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 size1 = s1.trim().chars().count(); let size2= s2.trim().chars().count(); match size1.cmp(&size2) { 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 sz1 = s1.trim().chars().count(); let sz2 = s2.trim().chars().count(); match sz1.cmp(&sz2) { std::cmp::Ordering::Less => Some(s2), std::cmp::Ordering::Equal => None, std::cmp::Ordering::Greater => Some(s1), }}
// 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 s1_size = s1.trim().chars().count(); let s2_size = s2.trim().chars().count(); match s1_size.cmp(&s2_size) { Ordering::Less => Some(s2), Ordering::Equal => None, Ordering::Greater => 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_size = s1.trim().chars().count(); let s2_size = s2.trim().chars().count(); if s1_size > s2_size { Some(s1) } else if s2_size > s1_size { Some(s2) } else { 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 str1_len = s1.trim().chars().count(); let str2_len = s2.trim().chars().count(); match str1_len.cmp(&str2_len) { Ordering::Less => Some(s2), Ordering::Equal => None, Ordering::Greater => Some(s1), } }
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), } }
// 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() { 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> { let s1 = s1.trim(); let s2 = s2.trim(); if s1.chars().count() > s2.chars().count() { Some(s1) } else if s1.chars().count() < s2.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 len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 > len2 { Some(s1) } else if len2 > len1 { Some(s2) } else { None }}#[cfg(test)]mod test { use crate::longer_wish; #[test] fn test_length() { assert_eq!(longer_wish("hi", "hi"), None); assert_eq!(longer_wish("hi", "hello"), Some("hello")); assert_eq!(longer_wish("hello", "hi"), Some("hello")); } #[test] fn test_unicode() { assert_eq!(longer_wish("ᔐ", "hi"), Some("hi")); }}
// 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 { Some(s1) } else if len_s2 > len_s1 { Some(s2) } else { None // Strings are equal length }}
// 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 len_s1 = s1.trim().chars().count(); let len_s2 = s2.trim().chars().count(); if len_s1 == len_s2 { None } else if len_s1 < len_s2 { 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 len_s1 = s1.trim().chars().count(); let len_s2 = s2.trim().chars().count(); if len_s1 > len_s2 { Some(s1) } else if len_s1 < len_s2 { 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 len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 > len2 { Some(s1) } else if len2 > len1 { 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> { 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> { // Your code here let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 > l2 { Some(s1) } else if l1 < l2 { Some(s2) } else { None }}
// 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_len : usize = s1.trim().chars().count(); let s2_len : usize = s2.trim().chars().count(); match s1_len.cmp(&s2_len) { Ordering::Less => Some(s2), Ordering::Greater => Some(s1), 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> { // Your code here let (l1, l2) = (s1.trim().chars().count(), s2.trim().chars().count()); if l1 == l2 { return None; } if l1 > l2 { 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> { // Your code here let l1 = s1.trim().chars().count(); let l2 = s2.trim().chars().count(); if l1 == l2 { return None; } if l1 > l2 { 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_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) }}
// 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) }}
// 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 = |s:&str| s.trim().chars().count(); if(size(s1) > size(s2)) { Option::Some(s1.trim()) } else if(size(s1) < size(s2)) { Option::Some(s2.trim()) } else { Option::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()) { Option::Some(s1.trim()) } else if(s1.trim().chars().count() < s2.trim().chars().count()) { Option::Some(s2.trim()) } else { Option::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 = |s:&str| s.trim().chars().count(); if size(s1) < size(s2){ Some(s2) }else if size(s2) < size(s1){ Some(s1) }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> { let s1_count = s1.trim().chars().count(); let s2_count = s2.trim().chars().count(); if s1_count > s2_count { return Some(s1); } if s1_count < s2_count { return Some(s2); } None}
pub fn longer_wish<'a>(s1: &'a str, s2: &'a str) -> Option<&'a str> { let s1_trimmed = s1.trim().chars().count(); let s2_trimmed = s2.trim().chars().count(); if s1_trimmed == s2_trimmed { None } else if s1_trimmed > s2_trimmed{ 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 = s1.trim(); let s2 = s2.trim(); let len1 = s1.chars().count(); let len2 = s2.chars().count(); if len1 > len2 { Some(s1) } else if len2 > len1 { 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> { use std::cmp::Ordering; let s1 = s1.trim(); let s2 = s2.trim(); match s1.chars().count().cmp(&s2.chars().count()) { Ordering::Equal => None, Ordering::Less => Some(s2), Ordering::Greater => Some(s1) }}
fn main() { println!("Hello, world!");}// 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_taille = s1.trim().chars().count(); let s2_taille = s2.trim().chars().count(); if s1_taille > s2_taille { return Some(&s1); } else if s1_taille < s2_taille { 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 s1Size = s1.trim().chars().count(); let s2Size = s2.trim().chars().count(); if s1Size == s2Size { return None; } if s1Size > s2Size { return Some(&s1); } 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 = s1.trim(); let s2 = s2.trim(); if s1.chars().count() == s2.chars().count() { return None } if s1.chars().count() > s2.chars().count() { return Some(s1) } 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 len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 == len2 { return None; } else if len1 > len2 { 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> { // Your code here let s1 = s1.trim(); let s2 = s2.trim(); if s1.chars().count() > s2.chars().count() { Some(s1) } else if s2.chars().count() > s1.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 len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 > len2 { return Some(s1); } else if len2 > len1 { 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 len1 = s1.trim().chars().count(); let len2 = s2.trim().chars().count(); if len1 > len2 { return Some(s1); } else if len2 > len1 { 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 s1_us = s1.trim().chars().count(); let s2_us = s2.trim().chars().count(); if s1_us > s2_us{ Some(s1) } else if s2_us > s1_us { Some(s2) } else { None }}