Rust Lifetimes

Created: before i made this website

  • lifetimes = how long references are valid
  • prevent dangling references (memory safety)
  • every reference has a lifetime (compiler tracks it)
  • most lifetimes are inferred automatically
  • explicit lifetimes use 'a, 'b, etc
  • needed when returning references or storing refs in structs
  •   fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
    • expl: return ref will be valid as long as x & y are valid
  • structs with refs need lifetime annotations:
    • struct foo<'a> { part: &'a str }
  • lifetimes only exist at compile time
  • they describe validity; they don’t extend object life