C++ Smart Pointer versus RUST ownership and borrowing

This week I had a nice discussion on Smart Pointer versus RUST. My takeaways are the following.

FeatureC++ Smart PointersRust Ownership & Borrowing
(also has SmartPointer)
GoalManage dynamic memory with RAII – “Resource Acquisition Is Initialization”Ensure memory safety at compile time
Core ConceptPointer with automatic resource releaseOwnership rules enforced by the compiler
Memory SafetyRuntime safety via smart pointersCompile-time safety via borrow checker

And while programming

ConceptC++Rust
Unique ownershipstd::unique_ptr<T>Box<T> (heap allocation) + ownership transfer
Shared ownershipstd::shared_ptr<T>Rc<T> (single-threaded), Arc<T> (thread-safe)
Non-owning referenceRaw pointer or std::weak_ptr<T>&T (immutable borrow), &mut T (mutable borrow)
Null safetynullptr possibleNo null references (Option<T> is used instead)

Code example:

#include <memory>

std::unique_ptr<int> makeInt() {
    return std::make_unique<int>(42);
}

int main() {
    std::unique_ptr<int> ptr = makeInt();
    // *ptr = 10; // OK
}
fn make_int() -> Box<i32> {
    Box::new(42)
}

fn main() {
    let ptr = make_int();
    // *ptr = 10; // OK
}

Same ease of use

Safety considerations

CategoryC++Rust
Dangling pointersPossible if raw pointers or misused shared_ptrPrevented by ownership rules
Use-after-freePossible, unless smart pointers are carefully usedStatistically eliminated by borrow checker
Data racesPossible in multithreaded contextsCompiler prevents them via aliasing rules
Reference countingRuntime cost with shared_ptr/weak_ptrRuntime cost with Rc/Arc, compile-time borrow checks otherwise
Zero-cost abstractionNot guaranteedStrong focus on zero-cost abstractions

Reference Cycles

But also consider this nice chapter in the RUST handbook: Reference Cycles Can Leak Memory – The Rust Programming Language

Even though Rust enforces strict memory safety at compile time, reference cycles are still possible when using reference-counted types like Rc<T> or Arc<T>—similar to shared_ptr in C++. This is one of the few scenarios in which Rust can leak memory, albeit safely (i.e., without undefined behavior).

ConceptC++Rust
Reference cyclesCan occur with std::shared_ptrCan occur with Rc<T> or Arc<T>
PreventionUse std::weak_ptr to break cyclesUse Weak<T> to break cycles
Memory leak riskYes, if cycles are not brokenYes, in same cases — logic errors, not unsafe
Runtime check?No automatic detectionNo automatic detection eitherEven though Rust enforces strict memory safety at compile time, reference cycles are still possible when using reference-counted types like Rc<T> or Arc<T>—similar to shared_ptr in C++. This is one of the few scenarios in which Rust can leak memory, albeit safely (i.e., without undefined behavior).
Concept
C++
Rust
Reference cycles
Can occur with std::shared_ptr
Can occur with Rc<T> or Arc<T>
Prevention
Use std::weak_ptr to break cycles
Use Weak<T> to break cycles
Memory leak risk
Yes, if cycles are not broken
Yes, in same cases — logic errors, not unsafe
Runtime check?
No automatic detection
No automatic detection either
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *