Rust: Working of Memory Management along with Ownership in Rust in compare to C++ & Python

Shobhit chaturvedi
4 min readApr 17, 2023

--

Safety and Controls are two important aspects of developing scalable large software system , architects often do tradeoff between programming languages which are more safe e.g. Python , Node Js, Java , C# etc. and programming languages which provide more controls e.g. C/C++ etc.

Programming language which are safe will compromise with Speed on the other hand programming languages which provides more controls will compromise with safety though they have speed.

Rust promise both Safety and Controls by adopting best of the both worlds of programming languages i.e. as performant as C/C++ at the same time it has safety of python and C#.

Trade off between Safety and Control

Ownership model in Rust is the key feature which create this magic lets understand how Ownership model in Rust play an important role in Memory management of datatypes like String and Vectors.

First lets understand how Rust identify and Restrict developers to create dangling pointer at compile time , it is the first step towards safe code

if we write above code in other programing language(e.g. C/C++) it will create run time error as we are trying to access a vector after dropping it, but Rust compiler is smart enough to catch this and throw the error while building the code.

lets focus on how Rust allocate and deallocate memory without garbage collector and provide strength to compiler without compromising performance at run time.

As shown in picture below in C++ whenever we try to create new vector by assigning data from existing vector , deep copy comes into play and allocate memory in heap each time.

On the other hand in case of programming language like python , object assignment works based on reference count model (shared ownership model) i.e. instead of allocating memory in heap ,python compiler increase the reference count of variable and GC (garbage collector) will do the rest for deallocating it.

In Rust we have freedom of doing both based on our need but by default it provide unique way to handle this situation lets see now -

variable ‘s’ will get memory in heap as shown below and as soon as we assign ‘ s’ to ‘t ’ in next line ‘s’ will transfer its ownership to ‘t’ and ‘s’ will become empty, Rust compiler knows it that’s why it generate error at compile time if we try to access ‘s’ again after it become empty by transferring its ownership to ‘t’

If we really want to assign ‘s’ to ‘t’ and ‘u’ without transferring ownership ,Rust will help us with two option either we can go with creating memory from heap similar to C++ way or we can go with the increasing reference count each time we assigning ‘s’ to ‘t’ and ‘u’

below code shows In Rust, how one can assign value to other variable without loosing it ownership like the concept called deep copy in C++.

above code works fine without any error.

You could also recreate Python’s behavior by using Rust’s reference counted pointer types as shown in code below also known as Shared Ownership : RC and Arc -

The Rc and Arc types are very similar; the only difference between them is that an Arc is safe to share between threads directly — the name Arc is short for atomic reference count — whereas a plain Rc uses faster nonthread-safe code to update its reference count. If you don’t need to share the pointers between threads, there’s no reason to pay the performance penalty of an Arc, so you should use Rc; Rust will prevent you from accidentally passing one across a thread boundary.

This shows the capability of Rust as programming language which include best of both worlds to reduce cost of software maintenance by writing safe and controlled code.

--

--