Memory, safety and Rust ownership · Part 27 of 36
Rust ownership and borrowing
Expert 20 min
Ownership, moves, borrowing, lifetimes and the borrow checker — how Rust gives memory safety without a garbage collector.
What you will learn
- You can state Rust’s three ownership rules.
- You can explain moves, shared and mutable borrows.
- You can read simple lifetime errors and fix them.
Before this: Memory safety and why it matters for security
Safety without a garbage collector
Rust aims for the speed and control of C++ and the memory safety of managed languages. Instead of a GC at run time, the compiler checks a set of ownership rules at compile time. Code that could cause a use-after-free or a data race simply does not compile.

The three rules
- Every value has exactly one owner (a variable).
- There can only be one owner at a time.
- When the owner goes out of scope, the value is dropped (freed).
fn main() {
let a = String::from("kale"); // a owns the heap string
let b = a; // ownership MOVES to b
// println!("{a}"); // error: borrow of moved value `a`
println!("{b}");
} // b goes out of scope: string freed once
Because only b owns the string, it is freed exactly once — no double free. Small types such as integers implement Copy and are copied instead of moved. For an explicit deep copy you call .clone().
Borrowing
Passing ownership around for every call would be clumsy, so you borrow with references:
fn length(s: &String) -> usize { s.len() } // shared borrow: read only
fn shout(s: &mut String) { s.push('!'); } // mutable borrow: may change
let mut crop = String::from("leek");
let n = length(&crop);
shout(&mut crop);
The borrowing rule is the heart of Rust:
At any time you may have either any number of shared references (
&T) or exactly one mutable reference (&mut T) — never both.
This "aliasing XOR mutability" rule prevents whole bug classes: you can't modify a vector while iterating over it (which could move its buffer and leave the iterator dangling), and two threads can't write the same data without synchronisation — data races become compile errors.
Lifetimes
A reference must never outlive the data it points to:
let r;
{
let x = 5;
r = &x; // error: `x` does not live long enough
}
println!("{r}");
The compiler tracks lifetimes — the regions of code where references are valid. Usually it infers them; in function signatures that return references you sometimes write them out: fn longest<'a>(a: &'a str, b: &'a str) -> &'a str says "the result lives as long as both inputs".
Shared ownership and threads
When data really needs several owners, Rust offers explicit tools: Rc<T> (reference counted, single thread), Arc<T> (atomic reference counting across threads), and Mutex<T>/RwLock<T> for shared mutable state. The type system tracks which types may be sent to (Send) or shared between (Sync) threads, so "fearless concurrency" is checked at compile time.
unsafe
Some low-level code (talking to hardware, calling C) cannot be checked. Rust allows it inside unsafe blocks, which mark exactly where reviewers must look; safe abstractions wrap them. The Rust 2024 edition (released with Rust 1.85 in February 2025) tightened this, for example requiring unsafe extern blocks and making std::env::set_var an unsafe function.
The cost
The borrow checker has a learning curve: designs with many objects pointing at each other (graphs, doubly linked lists) need indices, arenas or Rc/RefCell. In exchange you get C-like performance, predictable memory use without GC pauses, and a compiler that catches whole classes of bugs.
Key points
- Each value has one owner; when the owner leaves scope, the value is dropped.
- Assignment moves ownership (except for Copy types);
.clone()copies explicitly. - Either many
&Tor one&mut T— never both: no dangling iterators, no data races. - Lifetimes ensure references never outlive their data.
- Rc/Arc/Mutex give explicit shared ownership;
unsafemarks unchecked code. Rust 2024 edition: Rust 1.85, Feb 2025.
Flashcards
- Question: What happens to
aafterlet b = a;with a String? - Answer: Ownership moved to b; using a is a compile error.
- Question: State Rust’s borrowing rule.
- Answer: Either any number of shared references or exactly one mutable reference at a time.
- Question: What prevents a reference from outliving its data?
- Answer: Lifetime checking by the borrow checker.
- Question: Which type gives shared ownership across threads?
- Answer: Arc<T> (often with Mutex<T> for mutation).
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
let a = String::from("x"); let b = a; println!("{a}"); — what happens?
- Compile error: a was moved
- It prints x
- It prints an empty string
- Run-time crash
Show the answer
Compile error: a was moved
Ownership moved to b, so the compiler rejects using a.
Source: The Rust Programming Language, 4.1
Which combinations of references to the same value are allowed at the same time?
- Three shared references &T
- One mutable reference &mut T
- One &mut T and one &T
- Two &mut T
Show the answer
Three shared references &T; One mutable reference &mut T
Many readers or one writer — never mixed.
Source: The Rust Programming Language, 4.2
Which Rust release shipped the 2024 edition?
- Rust 1.85 (February 2025)
- Rust 1.0 (2015)
- Rust 1.56 (2021)
- Rust 2.0
Show the answer
Rust 1.85 (February 2025)
The Rust 2024 edition was stabilised in Rust 1.85.0 on 20 February 2025.
Source: Rust Blog, Announcing Rust 1.85.0
When the owner goes out of scope, Rust ___ the value (frees it).
Show the answer
drops
Values are dropped at the end of their owner’s scope.
Source: The Rust Programming Language, 4.1
Sources
- The Rust Programming Language (Rust 2024 edition), Klabnik, Nichols & Krycho, Rust Project, 2025
- The Rust Reference, Rust Project, 2025
- Rust Blog: Announcing Rust 1.85.0 and Rust 2024, Rust Release Team, 2025
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.