The Rust Programming Language — Lifetime — Lifetime, Lifetime Annotations, Lifetime Elision and Static Lifetime
Diving very deep into Lifetimes! Understand how Rust’s Memory Management System works.
7 min readOct 23, 2023
Lifetime:
A lifetime is the period starting from when a variable is allocated and de-allocated from the memory. Refer the below diagram:
This is the last topic in this series which emphasises on how Rust’s Memory Management System works. We’ll start with below code snippet:
struct Releases {
years: &[i64],
eighties: &[i64],
nineties: &[i64],
}
fn main() {
let years: Vec<i64> = vec![1988, 1989, 1990, 1991, 2004, 2006];
let eighties: &[i64] = &years[0..2];
let nineties: &[i64] = &years[2..4];
}
The code snippet is fairly simple. We have a years
vector and eighties
and nineties
references from the years
vector. We also have a struct Releases
which has years
, eighties
, and nineties
as the members of the struct which are also nothing but the reference of the memory where years
is stored in the heap.