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.

Step 1 / 2

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.
11_lifetimes.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() >= y.len() { x } else { y }
}