Lifetime - the amount of time in a program where a specific piece of data is valid.
The Borrow Checker evaluates the lifetimes of values in order to make sure the Borrowing rules are not being broken.
the goal is to check if:
The references lifetime does not outlive the owner value's lifetime (the original)
Last Example:
Concrete vs Generic Lifetimes
concrete == the span of code where the ownership is valid and that the compiler infers.
but generic lifetimes are a type of generic that inform the compiler how references to data should be related to each other and ensuring that data lives long enough to be accessed.
take these for example
the concrete lifetime annotation is 'static
Concrete Lifetimes
Generic lifetime
Here, 'a
is a generic lifetime. It doesn’t specify how long the reference will live; it only guarantees that result
will not outlive input
.
In short:
- Concrete lifetimes are explicit (like
'static
). - Generic lifetimes allow for flexibility in how long references can live while ensuring safety.
These annotations help the compiler understand that we need to put the variables with extended lifetimes on the Heap, because normally they would