Error

"cannot infer an appropriate lifetime" — How to Fix

Fix the 'cannot infer an appropriate lifetime' error by adding explicit lifetime parameters to your function signature.

Add explicit lifetime parameters to your function signature to tell the compiler how long the returned reference is valid.

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

The 'a lifetime parameter ensures the returned reference lives as long as the shortest of the two input references.