DEV Community

Cover image for Understanding the notion of references in rust 🦀
Soumajit Das
Soumajit Das

Posted on

Understanding the notion of references in rust 🦀

Hey, are you trying to learn Rust and want to dig deep into the realms of memory management in Rust? then this post is for you.
In this post, I'm going to take you through the rarified concepts of references in the Rust programming language.

I'm learning rust these days as well, so if you find me wrong anywhere, correct me in the comment section, and if you find this post helpful, do let me know in the comment section 😊. Let's get started.

What does reference mean in rust? ?

In rust, a reference is like a pointer that holds an address, which we can follow to access the data stored at that address, and the data is owned by some other variable.
In other words, we can say that

  1. A reference consists of borrowed data with an address for the actual data.
    attached to it.

  2. The borrowing happens with the help of the address. A reference to a variable can borrow data by accessing the address, and the address helps the reference reach out to the data, thus making the data usable without any ownership.
    lets understand it by using an simple code example:

fn main(){
      let actual_data = 4;
      let reference_of_actual_data = &actual_data;
     }
Enter fullscreen mode Exit fullscreen mode

So in the above code, I've declared a variable called actual_data, and it is assigned to an unsigned integer value, which is 4, and notice that the variable is immutable, which means we cannot assign any other value to it. And in the next line, another variable is declared called reference_of_actual_data, and it has something assigned to it as well, which is &actual_data. And this is the reference to the actual_data that we've declared earlier.

So how does this work?

The very idea of reference comes into play when we don't want the ownership of our declared variables to go to the hands of another variable. In the above code snippet, that's what's happened. After declaring the second variablereference_of_actual_data, we are not changing the ownership of the value 4; rather, the owner of the value, which is actual_data, remains the same, but by assigning, we are creating a reference to that value and borrowing the value from the reference_of_actual_data variable. And when the main() scope ends, reference_of_actual_data goes out of scope instantly.

But we got to stay sane enough when we're dealing with memory allocation while playing with rust. In the above code, we've dealt with primitive scalars, or, in other words, integers. In rust, an integer is always going to use stack allocation, unlike Strings, which are going to get stored in memory via heap allocation.

Mutable and Immutable reference

In the above example, we saw how references work, but as they say, there have to be some types and classifications, and references are not an exception either.In the above code snippet, we played with a type of reference called immutable references. Yes, rust follows the concepts of mutability and immutability in the references too.

So far, we couldn't change the value of the owner as well as the reference, but to make our references mutable and for the sake of modification as well, there's another kind of reference that exists, called mutable reference. lets understand that with another code example.

fn main() {
    let mut x = 4;   
    let ref_x = &mut x; // here we are creating a mutable reference
    *ref_x = 3; //modifying the value through the mutable reference
    println!("The value of ref_x is: {ref_x}")
}
Enter fullscreen mode Exit fullscreen mode

Generally, to modify the borrowed value that the reference was pointing to, we need to make the reference as well as the actual owner of the value mutable. And to make the reference mutable, we use &mut. So in the code snippet, ref_x is a mutable reference to x, which is mutable as well.
Now comes the fun part: to modify the value of ref_x, we've got to use *, which is called the dereference operator. This helps us to access the value of the owner through the reference, and voila 🚀, we have successfully modified the value.

Note: The value of ref_x as well as x is 3 now, because both of them pointing to the same memory location.

Conclusion

I hope the explanation was vivid enough for you to understand the concepts of reference with utter ease.
And hey, if you like this explanation, do share your valuable insights in the comment section.
Happy Coding to all of you.

Top comments (2)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Awesome post, Soumajit! 🦀

Collapse
 
dev_sd profile image
Soumajit Das

Thank you so much Michael 😊🦀