Konubinix' opinionated web of thoughts

Rust Lifetime

fleeting

references here must outlive this structure

https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime/struct.html

  • 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.

https://doc.rust-lang.org/rust-by-example/scope/lifetime/lifetime_bounds.html

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.

https://doc.rust-lang.org/rust-by-example/scope/lifetime/fn.html

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

https://doc.rust-lang.org/rust-by-example/scope/lifetime/fn.html

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.

https://doc.rust-lang.org/rust-by-example/scope/lifetime.html

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.

https://doc.rust-lang.org/stable/book/ch10-03-lifetime-syntax.html

how generic lifetime parameters of multiple references relate to each other

https://doc.rust-lang.org/stable/book/ch10-03-lifetime-syntax.html

Notes linking here