Konubinix' opinionated web of thoughts

Rust Lifetime


references here must outlive this structure


  • T: ‘a: All references in T must outlive lifetime ‘a.
  • T: Trait + ‘a: Type T must implement trait Trait and all references

in T must outlive ‘a.


function signatures with lifetimes have a few constraints:

any reference must have an annotated lifetime. any reference being returned must have the same lifetime as an input or be static.


note that returning references without input is banned if it would result in returning references to invalid data


A lifetime is a construct the compiler (or more specifically, its borrow checker) uses to ensure all borrows are valid. Specifically, a variable’s lifetime begins when it is created and ends when it is destroyed. While lifetimes and scopes are often referred to together, they are not the same.


Lifetime annotations don’t change how long any of the references live. Just as functions can accept any type when the signature specifies a generic type parameter, functions can accept references with any lifetime by specifying a generic lifetime parameter. Lifetime annotations describe the relationships of the lifetimes of multiple references to each other without affecting the lifetimes.


how generic lifetime parameters of multiple references relate to each other


Notes linking here