ganeshn2blog.
← Back to the journal
Learning · Rust & C++ ·

Who owns the memory? What I love about Rust and C++

Ownership, RAII, and what my two favourite languages teach me about managing resources.

Rust and C++ are my two favourite programming languages. One connection between them that I find especially interesting is how they give resources a clear lifetime.

While reading about Rust’s ownership model, I came across a familiar idea from C++: RAII, short for Resource Acquisition Is Initialization.

The name sounds complicated, but the principle is practical. An object takes responsibility for a resource, and its destructor releases that resource when the object’s lifetime ends. Rust follows a similar approach with automatic dropping. When an owned String goes out of scope, its allocated memory is released. If ownership moves elsewhere, responsibility moves with it.

What I particularly love about Rust is how much the compiler checks around that responsibility.

Imagine borrowing a reference to some text, destroying the text, and then trying to read through that reference. C++ resource cleanup alone does not generally prevent this dangling-reference mistake. Safe Rust rejects it because the borrowed reference cannot outlive its owner.

That is the distinction that makes ownership so compelling to me. Cleanup answers, “Who releases this memory?” Borrow checking also asks, “Can someone still be using it?”

Neither approach needs a tracing garbage collector for this kind of resource management. Both offer control and predictable cleanup, and the idea extends beyond memory to files and locks.

I admire C++ for its flexibility and the strength of RAII. I love Rust for making ownership relationships something the compiler helps enforce. Understanding the connection gives me another reason to appreciate both.