---
title: "How does ownership prevent memory leaks and data races?"
description: "How the ownership rules close off memory leaks and data races before the program ever runs, and where they stop short."
url: "https://mayorana.ch/en/blog/ownership-safety-rust"
language: "en"
date: "2025-08-02"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "memory"
  - "ownership"
  - "borrowing"
  - "data-races"
fr_version: "https://mayorana.ch/fr/blog/ownership-safety-rust-fr.md"
---

# How does ownership prevent memory leaks and data races?

Ownership is Rust's core memory management system, enforcing strict rules at compile time to ensure safety without a [garbage collector](https://mayorana.ch/en/blog/why-garbage-collector). It prevents memory leaks and data races through a combination of ownership rules, move semantics, and borrowing.

<div class="svg-container" style="margin:2rem 0;">
<svg class="mm4-fig" viewBox="0 0 800 240" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Moving a String from s1 to s2 transfers ownership of the heap data and invalidates s1">
<style>
.mm4-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .mm4-fig,[data-theme="dark"] .mm4-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.mm4-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.mm4-fig .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.mm4-fig .deadbox{fill:none;stroke:var(--mut);stroke-width:1.5;stroke-dasharray:4 3}
.mm4-fig .title{font:700 14px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm4-fig .body{font:600 12px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm4-fig .cap{font:11px ui-sans-serif,system-ui,sans-serif;fill:var(--mut)}
.mm4-fig .ac{fill:var(--ac)}
.mm4-fig .mut{fill:var(--mut)}
.mm4-fig path{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="mm4-arrow" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ac)"/></marker>
</defs>
<!-- s1 box, now invalid -->
<rect x="40" y="40" width="200" height="70" rx="8" class="deadbox"/>
<text x="140" y="66" text-anchor="middle" class="body mut">s1 (moved)</text>
<text x="140" y="84" text-anchor="middle" class="cap">invalid: use after move</text>
<!-- s2 box, valid owner -->
<rect x="330" y="40" width="200" height="70" rx="8" class="acbox"/>
<text x="430" y="66" text-anchor="middle" class="title ac">s2</text>
<text x="430" y="84" text-anchor="middle" class="cap">new owner of the String</text>
<!-- heap box -->
<rect x="590" y="40" width="180" height="70" rx="8" class="box"/>
<text x="680" y="66" text-anchor="middle" class="body">"hello"</text>
<text x="680" y="84" text-anchor="middle" class="cap">heap buffer (unchanged)</text>
<!-- arrows -->
<path d="M240,75 L330,75" marker-end="url(#mm4-arrow)"/>
<text x="285" y="65" text-anchor="middle" class="cap">move</text>
<path d="M530,75 L590,75" style="stroke:var(--ac)" marker-end="url(#mm4-arrow)"/>
<!-- scope note -->
<rect x="40" y="150" width="730" height="60" rx="8" class="box"/>
<text x="405" y="175" text-anchor="middle" class="body">take_ownership(s)  →  s dropped when function scope ends</text>
<text x="405" y="193" text-anchor="middle" class="cap">only one owner exists at a time: no double free, no leak</text>
</svg>
</div>

## Ownership in Rust

- Each value has a **single owner** (variable).
- When the owner goes out of scope, the value is **dropped** (`Drop` trait called).
- Ownership can be **transferred** (moved), making the original variable invalid.

## Key Rules

### Move Semantics

Assigning a heap-allocated value (e.g., `String`) to another variable transfers ownership, invalidating the original.

**Example**:
```rust
let s1 = String::from("hello");
let s2 = s1; // Ownership moved to s2
// println!("{}", s1); // Compile error: value borrowed after move
```

### Copy vs. Move

- Types with **known size** (`i32`, `bool`) implement `Copy` and are cloned automatically.
- Heap-allocated types (`String`, `Vec`) do not implement `Copy` and are moved.

### Function Calls

Passing a value to a function moves or copies it, following the same rules.

**Example**:
```rust
fn take_ownership(s: String) { /* ... */ }

let s = String::from("hello");
take_ownership(s); // Ownership moved into the function
// println!("{}", s); // Error: s is invalid
```

## How Ownership Prevents Memory Leaks

- **Automatic Cleanup**: When the owner goes out of scope, Rust calls `drop` to free memory (no manual `free()` needed).
- **No Double Frees**: Since only one owner exists, the value is dropped exactly once.

## How Ownership Prevents Data Races

- **Borrowing Rules**:
  - **Immutable borrows** (`&T`): Multiple allowed, but no mutable borrows can coexist.
  - **Mutable borrows** (`&mut T`): Only one allowed, and no other borrows can exist.
- **Compile-Time Enforcement**: The compiler rejects code that could lead to data races.

**Example: Data Race Prevention**:
```rust
let mut data = vec![1, 2, 3];

let r1 = &data; // Immutable borrow OK
let r2 = &data; // Another immutable borrow OK
// let r3 = &mut data; // ERROR: Cannot borrow as mutable while immutable borrows exist

println!("{:?}, {:?}", r1, r2);
```

<div class="svg-container" style="margin:2rem 0;">
<svg class="mm4-fig2" viewBox="0 0 800 320" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Matrix of which borrow you can take depending on the borrows already live, highlighting that a mutable borrow is rejected while shared borrows exist">
<style>
.mm4-fig2{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .mm4-fig2,[data-theme="dark"] .mm4-fig2{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.mm4-fig2 .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.mm4-fig2 .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.mm4-fig2 .title{font:700 14px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm4-fig2 .head{font:700 13px ui-sans-serif,system-ui,sans-serif;fill:var(--mut)}
.mm4-fig2 .body{font:600 12px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm4-fig2 .cap{font:11px ui-sans-serif,system-ui,sans-serif;fill:var(--mut)}
.mm4-fig2 .ac{fill:var(--ac)}
.mm4-fig2 path{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="mm4b-arrow" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ln);stroke:none"/></marker>
<marker id="mm4b-arrow-ac" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ac);stroke:none"/></marker>
</defs>
<!-- title -->
<text x="400" y="24" text-anchor="middle" class="title">What the borrow checker allows next</text>
<!-- column headers -->
<text x="50" y="80" class="head">Borrows already live</text>
<text x="425" y="80" text-anchor="middle" class="head">you ask for &amp;data</text>
<text x="625" y="80" text-anchor="middle" class="head">you ask for &amp;mut data</text>
<!-- row 1 -->
<text x="50" y="130" class="body">none</text>
<rect x="330" y="96" width="190" height="58" rx="8" class="box"/>
<text x="425" y="122" text-anchor="middle" class="body">allowed</text>
<text x="425" y="140" text-anchor="middle" class="cap">first shared borrow</text>
<rect x="530" y="96" width="190" height="58" rx="8" class="box"/>
<text x="625" y="122" text-anchor="middle" class="body">allowed</text>
<text x="625" y="140" text-anchor="middle" class="cap">exclusive access</text>
<!-- row 2 -->
<text x="50" y="190" class="body">r1 = &amp;data, r2 = &amp;data</text>
<text x="50" y="208" class="cap">any number of shared</text>
<rect x="330" y="162" width="190" height="58" rx="8" class="box"/>
<text x="425" y="188" text-anchor="middle" class="body">allowed</text>
<text x="425" y="206" text-anchor="middle" class="cap">shared borrows stack up</text>
<rect x="530" y="162" width="190" height="58" rx="8" class="acbox"/>
<text x="625" y="188" text-anchor="middle" class="body ac">rejected</text>
<text x="625" y="206" text-anchor="middle" class="cap">cannot borrow as mutable</text>
<!-- row 3 -->
<text x="50" y="256" class="body">m = &amp;mut data</text>
<text x="50" y="274" class="cap">exactly one</text>
<rect x="330" y="228" width="190" height="58" rx="8" class="box"/>
<text x="425" y="254" text-anchor="middle" class="body">rejected</text>
<text x="425" y="272" text-anchor="middle" class="cap">already mutably borrowed</text>
<rect x="530" y="228" width="190" height="58" rx="8" class="box"/>
<text x="625" y="254" text-anchor="middle" class="body">rejected</text>
<text x="625" y="272" text-anchor="middle" class="cap">only one &amp;mut at a time</text>
<!-- pointer to the highlighted cell -->
<path d="M760,191 L720,191" style="stroke:var(--ac)" marker-end="url(#mm4b-arrow-ac)"/>
<!-- caption -->
<text x="400" y="308" text-anchor="middle" class="cap">In the example r1 and r2 are still live, so `let r3 = &amp;mut data;` lands in the highlighted cell.</text>
</svg>
</div>

## Key Takeaways
**Ownership ensures**:
- No [dangling pointers](https://mayorana.ch/en/blog/dangling-pointer-rust) (via lifetimes).
- No memory leaks (via `Drop`).
- No data races (via borrowing rules).

Rust’s ownership model guarantees memory safety and concurrency safety at compile time, delivering performance and reliability.
