Coding/Learn/rust/ownership fundamentals
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnOwnership Rules & Move Semantics
7 / 20 0%
Lesson 07 of 20
7 mins read

Ownership Rules & Move Semantics

PreviousNext Lesson
In Plain English

Ownership is Rust’s signature feature that enables memory safety without a garbage collector. The three rules of ownership govern how heap allocations are tracked, transferred (moved), and automatically deallocated when an owner goes out of scope.

The Mental Model

"Ownership is like a physical library book: only one person can hold the physical book (owner) at a time. If you pass the book to a friend (move), you no longer possess it. When the last holder leaves the room, the book is returned to the shelf (drop)."

Deep Dive: How It Works

The 3 Rules of Ownership: 1) Each value has an owner. 2) There can only be one owner at a time. 3) When the owner goes out of scope, the value is dropped.

Move Semantics: Assigning a heap value (like String) to a new variable moves ownership. The original variable becomes invalid.

Stack vs Heap: Primitive scalar types implement Copy and duplicate on the stack; heap structures transfer ownership.

Syntax Blueprint

let s1 = String::from("hello"); let s2 = s1; // s1 is MOVED and invalid

Ownership of the heap buffer transfers to s2. Attempting to use s1 causes a compile-time error.

PointCore Rules to Remember

Point star
Single Owner: Every resource has exactly one owning variable.
Point star
Zero Memory Leaks: Drop runs automatically at closing brace } without manual free().
Point star
Explicit Clone: Use s1.clone() if you genuinely need a deep heap copy.

Common Beginner Traps & How to Fix Them

Mistake:Using a variable after it has been moved.

Why it happens: let s1 = String::from("hi"); let s2 = s1; println!("{}", s1); fails with E0382: borrow of moved value.

How to fix: Pass a reference (&s1) instead of moving ownership, or clone if an independent copy is needed.

Pro Tip: Primitive types (i32, bool, f64, char) implement Copy automatically—they are duplicated without moving!

Live Interactive Example

Hit Run Code to see it live
Move Semantics and Scope Drop
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates clone keeping original valid and passing ownership to a consuming function.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Clone a heap string to retain ownership

  • PointGiven let msg = String::from("ZenCompiler");
  • PointCreate let backup = msg.clone();
  • PointPrint "Primary: ZenCompiler | Backup: ZenCompiler".
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates