Rust is statically typed and there is a concept of Alias which states that data can be borrowed immutably but during borrowing, the original data cannot be borrowed mutably. Aliasing is useful for the optimization of data.
In Aliasing, only one data can be mutably borrowed at a time. On the condition that mutable reference has been used previously, only then the original data can be borrowed.
Let us see this example to understand Aliasing in Rust. We have three variables x,y, and z in Struct GFG, and variable1 and variable2 refer to GFG.
Now, data can be accessed via references (geeks_variable) and the original owner (GFG).
Example 1:
Rust
// Rust code for Aliasing
#![allow(dead_code)]
struct GFG {
// Default value of integer in integer is i32
a: i32,
b: i32,
c: i32
}
fn main() {
let geeks_variable = GFG { a: 1, b: 2, c: 3 };
let variable1 = &geeks_variable;
let variable2 = &geeks_variable;
println!("GFG has variables: ({}, {}, {})",
variable1.a, variable2.b, geeks_variable.c);
}
Output:
Here, we cannot borrow geeks_variable as mutable because it is currently borrowed as immutable. As immutable references are no longer used now, therefore we reborrow a mutable reference (mutable_reborrow_variable) and then we change the data using mutable references.
Example 2:
Rust
// Rust code
struct GFG {
// Default value of integer in integer is i32
a: i32,
b: i32,
c: i32
}
fn main() {
let mut geeks_variable = GFG { a: 1, b: 2, c: 3 };
let variable1 = &geeks_variable;
let variable2 = &geeks_variable;
println!("GFG has variables: ({}, {}, {})",
variable1.a, variable2.b, geeks_variable.c);
let mutable_reborrow_variable = &mut geeks_variable;
mutable_reborrow_variable.a = 10;
mutable_reborrow_variable.b = 20;
mutable_reborrow_variable.c = 30;
println!("GFG now has immutable variables: ({}, {}, {})",
mutable_reborrow_variable.a, mutable_reborrow_
variable.b, mutable_reborrow_variable.c);
let new_reborrow = &geeks_variable;
println!("GFG now has reborrowed variables: ({}, {}, {})",
new_reborrow.a, new_reborrow.b, new_reborrow.c);
}
Output:
Here, we see that we had passed mutable references to println! using the mutable_reborrow_variable. Again, we see the mutable reference is not used for the rest of the code and hence we reborrow it using the variable new_reborrow
Similar Reads
Rust - Casting Type Conversion or casting in Rust is a way to convert one type to another. As its name suggests, type conversion is the process of converting variables from one data type to another. So that, the compiler treats the variable as a new datatype. Rust doesn't allow us to implicitly convert the datatyp
3 min read
Rust - Literals A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix. Example:  Integer literal 6 h
1 min read
File I/O in Rust File I/O in Rust enables us to read from and write to files on our computer. To write or read or write in a file, we need to import modules (like libraries in C++) that hold the necessary functions. This article focuses on discussing File I/O in Rust. Methods for File I/O Given below are some method
7 min read
Rust - HashMaps The concept of HashMap is present in almost all programming languages like Java, C++, Python, it has key-value pairs and through key, we can get values of the map. Keys are unique no duplicates allowed in the key but the value can be duplicated. 1. Insertion In HashMap : To insert a value in a HashM
4 min read
Rust - Lifetime Lifetimes in Rust refer to the lifetime of how long a variable would live. Lifetimes are associated with references as references cannot live longer than the object from which it is derived. Lifetimes basically describe the scope that a reference is valid for. Lifetimes come into play when we are de
3 min read
Rust - Array An Array in Rust programming is a fixed-sized collection of elements denoted by [T; N] where T is the element type and N is the compile-time constant size of the array. We can create an array in 2 different ways: Simply a list with each element [a, b, c].Repeat expression [N, X]. Â This will create a
5 min read