Understanding ownership in Rust

Some days ago, I started learning Rust. I’m reading the official book “The Rust Programming Language” available here. When I arrived at chapter 4, I discovered how Rust manages memory and I decided to write this article to summarise what I learned.

Photo by Mae Mu on Unsplash

During university, I studied mainly two programming languages: C and Java. I still have some nightmares when I think about a big project I developed in C during a course because of memory management in C.
In C, the programmer has to manually allocate and deallocate the memory while in Java, there is a garbage collector that takes care of the memory management.

Rust has a completely different way to manage memory. It is called “ownership” and it guarantees memory safety without needing a garbage collector.

This system of ownership is based on three rules:

Let’s see an example to have a better understanding of the first two points:

From this example we can notice some things:

Now we can consider the third rule of the ownership system: “For each allocated space we can have one owner at a time”.
To understand this rule we can consider this example:

In this snippet, we define the variable s1 and we allocate some space on the heap to store the value, then we assign s1 to s2.
In other languages, we would have behaviour like the one we show in the image. Rust does a different thing.

The rule we wrote states that each allocated space can have at most one owner at a time, in this case, if we use the approach shown in the image, we would have two variables pointing to the same address on the heap. This would be a problem because we would have two owners for that space and when we reach the end of the scope we would drop the same space twice. This is obviously a bug and Rust doesn’t like it.
To avoid this problem, Rust implemented a “Move” operation, when we assign a variable to another variable as we did in the snippet, the first variable is “de-activated” and won’t point anymore to the address on the heap while the new variable will point to the address on the heap. In this way, we can avoid the double free.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store