← All Exercises
Rust Lifetime Annotations
intermediaterust
Background
Lifetime annotations express reference relationships that Rust cannot infer, ensuring borrow safety.
Problem
Write a Rust function that takes two string slices and returns the longer one, using explicit lifetime annotations.
Input
Two string slices with potentially different lifetimes.
Output
The longer string slice, with a lifetime tied to both inputs.
Sample tests
Sample 1
Input: "hello", "hi"
Expected: "hello"
Your solution
Learning objective
Understand lifetime annotations, the borrow checker rules for references, and lifetime elision limits.