Open In App

Rust – Concept of Data Freezing

Improve
Improve
Like Article
Like
Save
Share
Report

Rust is a systems programming language. In Rust, there is a concept of freezing which states that whenever data is borrowed, the borrowed data is also frozen. This frozen data cannot be modified until the references get out of scope i.e. all the bindings go out of scope.

Suppose in the given below example, we declare a mutable integer _mut_integer. Here, we are shadowing the immutable variable _mut_integer but we see that _mut_integer is already frozen in this scope as the data is bounded by the same name.

After the curly braces, _mut_integer goes out of scope.

Example 1:

Rust




fn main() {
    let mut _mut_integer = 8; 
    // mutable integer
  
    {
        // Borrow `integer`
        let _reference_to_integer = _mut_integer;
        // integer is frozen in this scope
        _mut_integer = 7;
        println!("{}", _reference_to_integer);
        println! ("{}", _mut_integer)
  
    // `reference_to_integer
    // goes out of scope beyond here
    }
    _mut_integer = 4; 
    // immutable integer
    println!("{}",_mut_integer);
}


Output:

 


Last Updated : 08 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads