Rust Fundamentals interview question · Most asked

What are lifetimes and when must you annotate them?

Create a free account to save this page, take notes and track your progress. Sign in

Summary

Lifetimes describe how long references are valid so the compiler can prove they never outlive their data. Most are inferred; you annotate when a function returns a reference that could come from several inputs, or when a struct stores references.

Longer Answer

Annotations don't change how long values live; they express relationships (e.g. the result lives as long as both inputs).

Key Points

  • Describe, don't extend, lifetimes.
  • Needed for ambiguous returned references and structs with references.
  • 'static lives for the whole program.

Deep dive: Rust Fundamentals: Lifetimes

More most-asked Rust Fundamentals questions

← All Rust Fundamentals questions

Was this page helpful?