---
title: "Rust: Memory Safety Without Garbage Collection"
description: "Rust gives you the performance of C with memory safety enforced at compile time. Learn how ownership and borrowing eliminate entire bug classes."
url: "https://mayorana.ch/en/blog/why-rust-memory-safe"
language: "en"
date: "2025-04-12"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "ownership"
fr_version: "https://mayorana.ch/fr/blog/why-rust-memory-safe-fr.md"
---

# Rust: Memory Safety Without Garbage Collection

Rust doesn't have a GC. It doesn't need one.

```rust
let msg = String::from("hello");
```

This allocates memory—but Rust tracks [ownership](https://mayorana.ch/en/blog/memory-safety-rust) statically.

## The Ownership Revolution

### Automatic Memory Management
```rust
fn greet() {
    let s = String::from("hello");
    // Use s...
} // s is dropped here automatically - no manual free() needed
```

**What happens:**
1. Memory allocated when `s` is created
2. Memory automatically freed when `s` goes out of scope
3. **No GC thread running in background**
4. **No runtime overhead**

### No More Use-After-Free
```rust,compile_fail
fn main() {
    let r;
    {
        let s = String::from("hello");
        r = &s;  // Borrow s
    } // s goes out of scope here
    
    println!("{}", r); // ❌ Compile error: s doesn't live long enough
}
```

**Compiler message:**
```
error[E0597]: `s` does not live long enough
  --> src/main.rs:5:13
   |
5  |         r = &s;
   |             ^^ borrowed value does not live long enough
6  |     }
   |     - `s` dropped here while still borrowed
```

The bug is **caught at compile time**, not runtime.

## Borrowing: References Without Danger

### Immutable Borrowing
```rust
fn calculate_length(s: &String) -> usize {
    s.len()  // Can read s, but not modify it
} // s goes out of scope, but doesn't drop the String (it's just a reference)

fn main() {
    let s1 = String::from("hello");
    let len = calculate_length(&s1);  // Pass reference
    println!("Length of '{}' is {}.", s1, len);  // s1 still valid
}
```

### Mutable Borrowing with Rules
```rust
fn main() {
    let mut s = String::from("hello");
    
    let r1 = &mut s;  // Mutable borrow
    // let r2 = &mut s;  // ❌ Cannot have two mutable borrows
    // let r3 = &s;      // ❌ Cannot have immutable borrow while mutable exists
    
    r1.push_str(", world");
    println!("{}", r1);
}
```

**Rust's borrowing rules prevent:**
- [Data races](https://mayorana.ch/en/blog/ownership-safety-rust) at compile time
- [Dangling pointers](https://mayorana.ch/en/blog/dangling-pointer-rust)
- Iterator invalidation
- Thread safety issues

<div class="svg-container" style="margin:2rem 0;">
<svg class="wr3b-fig" viewBox="0 0 800 240" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Any number of shared references is allowed, or exactly one mutable reference, but mixing a mutable and a shared reference is rejected at compile time">
<!-- style -->
<style>
.wr3b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00;--bad:#e11d48}
:root.dark .wr3b-fig,[data-theme="dark"] .wr3b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.wr3b-fig .panel{fill:none;stroke:var(--ln);stroke-width:1.5}
.wr3b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.wr3b-fig .boxac{fill:var(--box);stroke:var(--ac);stroke-width:2}
.wr3b-fig .boxbad{fill:var(--box);stroke:var(--bad);stroke-width:2;stroke-dasharray:4 3}
.wr3b-fig .ti{fill:var(--tx);font:700 13px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.wr3b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.wr3b-fig .mut{fill:var(--mut);font:11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.wr3b-fig .bad{fill:var(--bad);font:700 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.wr3b-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.wr3b-fig .lnbad{stroke:var(--bad);stroke-width:1.5;fill:none;stroke-dasharray:4 3}
</style>
<!-- defs -->
<defs>
<marker id="wr3b-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
<marker id="wr3b-arrowbad" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--bad)"/></marker>
</defs>
<!-- panel 1: many readers -->
<rect x="25" y="40" width="235" height="145" rx="8" class="panel"/>
<text x="142" y="62" class="ti">Many readers — OK</text>
<rect x="40" y="76" width="60" height="26" rx="4" class="box"/>
<text x="70" y="94" class="tx">&amp;s</text>
<rect x="112" y="76" width="60" height="26" rx="4" class="box"/>
<text x="142" y="94" class="tx">&amp;s</text>
<rect x="184" y="76" width="60" height="26" rx="4" class="box"/>
<text x="214" y="94" class="tx">&amp;s</text>
<path d="M70,102 L70,112 L142,112" class="ln"/>
<path d="M142,102 L142,112" class="ln"/>
<path d="M214,102 L214,112 L142,112" class="ln"/>
<path d="M142,112 L142,120" class="ln" marker-end="url(#wr3b-arrow)"/>
<rect x="72" y="120" width="140" height="30" rx="5" class="box"/>
<text x="142" y="140" class="tx">String</text>
<text x="142" y="172" class="mut">nobody can change it — safe to share</text>
<!-- panel 2: one writer -->
<rect x="282" y="40" width="235" height="145" rx="8" class="panel"/>
<text x="399" y="62" class="ti">One writer — OK</text>
<rect x="354" y="76" width="90" height="26" rx="4" class="boxac"/>
<text x="399" y="94" class="tx">&amp;mut s</text>
<path d="M399,102 L399,120" class="ln" marker-end="url(#wr3b-arrow)"/>
<rect x="329" y="120" width="140" height="30" rx="5" class="box"/>
<text x="399" y="140" class="tx">String</text>
<text x="399" y="172" class="mut">exclusive — nobody else is looking</text>
<!-- panel 3: rejected -->
<rect x="539" y="40" width="235" height="145" rx="8" class="panel"/>
<text x="656" y="62" class="ti">Both at once — rejected</text>
<rect x="571" y="76" width="80" height="26" rx="4" class="boxbad"/>
<text x="611" y="94" class="tx">&amp;mut s</text>
<rect x="661" y="76" width="80" height="26" rx="4" class="boxbad"/>
<text x="701" y="94" class="tx">&amp;s</text>
<path d="M611,102 L611,112 L656,112" class="lnbad"/>
<path d="M701,102 L701,112 L656,112" class="lnbad"/>
<path d="M656,112 L656,120" class="lnbad" marker-end="url(#wr3b-arrowbad)"/>
<rect x="586" y="120" width="140" height="30" rx="5" class="boxbad"/>
<text x="656" y="140" class="tx">String</text>
<text x="656" y="172" class="bad">reader could see a torn value</text>
<!-- footer -->
<text x="400" y="222" class="mut">One rule covers all four bugs above: any number of readers, or exactly one writer — never both</text>
</svg>
</div>

## Real-World Comparison

### The Same Logic in Different Languages

**C version (unsafe):**
```c
char* process_data(char* input) {
    char* result = malloc(strlen(input) + 10);
    strcpy(result, input);
    strcat(result, " processed");
    return result;  // Caller must remember to free!
}

int main() {
    char* data = "hello";
    char* processed = process_data(data);
    printf("%s\n", processed);
    // Easy to forget: free(processed);
    return 0;
}
```

**Java version (GC overhead):**
```java
public String processData(String input) {
    return input + " processed";  // Creates temporary objects
}

public static void main(String[] args) {
    String data = "hello";
    String processed = processData(data);
    System.out.println(processed);
    // GC will eventually collect temporary objects
}
```

**Rust version (safe + fast):**
```rust
fn process_data(input: &str) -> String {
    format!("{} processed", input)  // Memory managed automatically
}

fn main() {
    let data = "hello";
    let processed = process_data(data);
    println!("{}", processed);
    // processed automatically dropped at end of scope
}
```

## Performance Characteristics

### Zero-Cost Abstractions
```rust
// High-level code...
let numbers: Vec<i32> = (0..1_000_000).collect();
let sum: i32 = numbers.iter().sum();

// ...compiles to the same assembly as:
let mut sum = 0;
for i in 0..1_000_000 {
    sum += i;
}
```

### Memory Layout Control
```rust
#[repr(C)]  // Same layout as C struct
struct Point {
    x: f32,
    y: f32,
    z: f32,
}

let points = vec![Point { x: 1.0, y: 2.0, z: 3.0 }; 1000];
// Contiguous memory layout, no GC overhead
```

## Thread Safety for Free

### Data Race Prevention
```rust
use std::thread;

fn main() {
    let data = vec![1, 2, 3, 4, 5];
    
    thread::spawn(move || {
        println!("Data: {:?}", data);  // data moved to thread
    });
    
    // println!("{:?}", data);  // ❌ Compile error: data was moved
}
```

### Safe Concurrent Access
```rust
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

    for _ in 0..10 {
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            let mut num = counter.lock().unwrap();
            *num += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    println!("Result: {}", *counter.lock().unwrap());
}
```

**No data races possible** - enforced at compile time.

## Language Feature Comparison

| Feature | Rust | C | Java | Python |
|---------|------|---|------|--------|
| Manual free | ❌ | ✅ | ❌ | ❌ |
| GC thread | ❌ | ❌ | ✅ | ✅ |
| Compile-time memory safety | ✅ | ❌ | ❌ | ❌ |
| Thread safety guarantees | ✅ | ❌ | ❌ | ❌ |
| Zero runtime overhead | ✅ | ✅ | ❌ | ❌ |
| Memory layout control | ✅ | ✅ | ❌ | ❌ |
| Prevents use-after-free | ✅ | ❌ | ✅ | ✅ |
| Prevents double-free | ✅ | ❌ | ✅ | ✅ |
| Prevents memory leaks | ✅ | ❌ | ✅* | ✅* |

*\*GC languages can still have memory leaks through references*

## The Rust Guarantee

### What Rust Eliminates
**Memory leaks** - automatic cleanup  
**Use-after-free** - ownership tracking  
**Double-free** - single ownership  
**Dangling pointers** - lifetime analysis  
**Buffer overflows** - bounds checking  
**Data races** - borrowing rules  
**Iterator invalidation** - compile-time checks  

### What You Get
**C-level performance**  
**Memory safety**  
**Zero runtime overhead**  
🔒 **Thread safety**  
🔧 **Systems programming capabilities**  

## Real-World Success Stories

### Dropbox Magic Pocket
- Replaced Python with Rust for storage system
- **Performance:** 10x improvement in CPU efficiency
- **Memory:** Predictable usage, no GC pauses
- **Reliability:** Eliminated entire classes of bugs

### Discord Chat Service
- Replaced Go with Rust for message handling  
- **Latency:** Consistent sub-millisecond response times
- **Memory:** Reduced memory usage by 40%
- **Scaling:** Handles millions of concurrent connections

### Mozilla Firefox
- Rust components in browser engine (Servo)
- **Security:** Eliminated memory safety vulnerabilities
- **Performance:** Faster rendering, lower memory usage

## The Paradigm Shift

### Traditional Approach
```
Fast code → Manual memory management → Bugs
Safe code → Garbage collection → Performance overhead
```

### Rust's Approach
```
Smart compiler → Ownership system → Fast + Safe code
```

<div class="svg-container" style="margin:2rem 0;">
<svg class="rustevo-fig" viewBox="0 0 800 260" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="The evolution from C, fast but unsafe, to garbage-collected languages, safe but slow, to Rust, which is both fast and safe">
<style>
.rustevo-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .rustevo-fig,[data-theme="dark"] .rustevo-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.rustevo-fig text{font-family:ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.rustevo-fig .title{font-size:15px;font-weight:700}
.rustevo-fig .body{font-size:12px;font-weight:600}
.rustevo-fig .cap{font-size:11px;fill:var(--mut)}
.rustevo-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.rustevo-fig .acbox{fill:var(--ac);stroke:var(--ac)}
.rustevo-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- defs -->
<defs>
<marker id="rustevo-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="var(--ln)"></path>
</marker>
</defs>
<!-- step 1: C -->
<rect class="box" x="20" y="80" width="210" height="110" rx="8"></rect>
<text x="125" y="108" text-anchor="middle" class="title">C</text>
<text x="125" y="130" text-anchor="middle" class="body">Fast</text>
<text x="125" y="148" text-anchor="middle" class="cap">manual memory mgmt</text>
<text x="125" y="163" text-anchor="middle" class="cap">unsafe — bugs at runtime</text>
<!-- step 2: GC languages -->
<rect class="box" x="295" y="80" width="210" height="110" rx="8"></rect>
<text x="400" y="108" text-anchor="middle" class="title">Java / Python / JS</text>
<text x="400" y="130" text-anchor="middle" class="body">Safe</text>
<text x="400" y="148" text-anchor="middle" class="cap">garbage collected</text>
<text x="400" y="163" text-anchor="middle" class="cap">slow — GC pause overhead</text>
<!-- step 3: rust accent -->
<rect class="acbox" x="570" y="80" width="210" height="110" rx="8"></rect>
<text x="675" y="108" text-anchor="middle" class="title" fill="#ffffff">Rust</text>
<text x="675" y="130" text-anchor="middle" class="body" fill="#ffffff">Fast AND Safe</text>
<text x="675" y="148" text-anchor="middle" class="cap" fill="#ffffff">ownership, no GC</text>
<text x="675" y="163" text-anchor="middle" class="cap" fill="#ffffff">compile-time guarantees</text>
<!-- arrows -->
<path class="ln" d="M230,135 L295,135" marker-end="url(#rustevo-arrow)"></path>
<path class="ln" d="M505,135 L570,135" marker-end="url(#rustevo-arrow)"></path>
<!-- caption -->
<text x="400" y="225" text-anchor="middle" class="cap">Rust isn't "safer C" — it's a different contract: no runtime needed to be safe</text>
</svg>
</div>

## What Rust actually buys you
🦀 **Rust gives you the best of both worlds:**

**Predictable performance** - no GC pauses, no runtime overhead  
**Memory safety** - entire bug classes eliminated at compile time  
**Fearless concurrency** - data races prevented by type system  
**Systems programming** - low-level control when needed  
**Modern ergonomics** - powerful type system, package management  

---

## TL;DR

**The Evolution:**
1. **C:** Fast but dangerous
2. **Java/Python/JS:** Safe but slow (GC overhead)
3. **Rust:** Fast AND safe (compile-time guarantees)

**Rust is not "safer C."** It's a fundamentally different contract:

> "You don't need a runtime to be safe—just a smart compiler."

**The Result:** Zero-cost memory safety. The holy grail of systems programming.

---

**Ready to eliminate entire bug classes from your code?** 
**→ Start learning Rust today.**
