Rust lifetimes describe relationships between the valid ranges of references. The compiler rejects a reference that could remain usable after its referent disappears. Writing a lifetime annotation does not extend a value’s life.
In the demo, the value’s bar must cover the reference bar. Extending the reference beyond that boundary reveals an invalid segment. Explicit lifetime parameters clarify which input reference a returned reference is tied to.
When to use
Use lifetimes when a function returns a reference or a struct connects multiple references.