Open In App

Rust lifetime Constructor

Last Updated : 27 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, we have a concept of a lifetime constructor that falls under the ownership category. Rust enforces a certain set of rules through its concept of ownership and lifetimes. In this article, we would be learning more about Lifetimes in Rust.

In Rust, there are various regions that are present in the code which are generally complex and referred to various parts of the program for which the references should be valid. Rust by default tags the references in the code to lifetimes so that there are no issues with respect to the validation of references. 

The rust compiler tracks the validity of references using the concept of borrowing and it helps the borrow checker it also ensures that we do not have any invalid references to the parts of the code.

Importance of Lifetime Constructor:

Lifetime constructor is an important aspect of Rust, as the rust compiler keeps all the existing references in control via lifetimes that are assigned to them. As, lifetimes come into play only during compile time therefore no run time errors occur. With the help of lifetime constructor, Rust always checks that we write code that do not have dangling pointers. Also, in certain situations whenever there are scoping issues, Rust compiler with the use of lifetime constructor can drop the values from memory and can create new scope.

Example 1:

Rust




fn main() {
{
    let variableOne;
    {                           
        let variableTwo = 100;
        variableOne = &variableTwo;
    }                   
  
    println!("The value of the first variable is {}.",variableOne);
}
}


Output:

 

Explanation:

In this example, whenever the inner scope is closed, variableTwo is dropped while it is still being borrowed. We can clearly see that variableOne is available in the outer scope. In this code, there are two distinct scopes: when variableTwo is dropped inner scope gets closed and at that point, we can clearly see variableOne is referred to its outer scope. Then, the reference becomes invalid as it is dropped from the scope that variableOne was referring to and hence as per the output we see the error that variableTwo does not live long enough.

Example 2: 

Rust




#![allow(unused)]
fn main() {
let mut vector_list = vec![10, 20, 30,40,50];
let addr = &vector_list[0];
vector_list.push(60);
println!("Value: {}", addr);
}


Output:

 

Explanation:

In this example, we have declared a vector_list that is mutable. The variable addr has the value of the address of the vector_list. We are trying to push an element to the vector_list but get an error there. There is a shared reference addr to a vector_list and here we are trying to take a mutable reference to vector_list for pushing. This results in an aliased mutable reference because ‘vector_list` is mutable in nature and it is being borrowed as an immutable. 

Example 3: 

Rust




#![allow(unused)]
fn main() {
let mut vector_list = vec![10, 20, 30,40,50];
let addr = &vector_list[1];
println!("Value: {}", addr);
vector_list.push(60);
}


Output:

 

Explanation:

In this example, the reference addr known as the borrow is still active from where it is created. This example is compiled without any error because after printing addr, it is not required anymore therefore the Rust compiler does not checks whether addr has dangling or aliased reference.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads