Core Advanced
11 Lifetimes
When a function or struct hands out a reference, the compiler needs to know how long it stays valid. Add the annotations that make these compile.
Add lifetimes so longest compiles and returns the longer &str.
Hints
- Tie inputs and output together:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str.
Reveal solution
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() >= y.len() { x } else { y }
}