Python Ownership Rules & Move Semantics
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.
"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 invalidOwnership of the heap buffer transfers to s2. Attempting to use s1 causes a compile-time error.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
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.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Clone a heap string to retain ownership
Given let msg = String::from("ZenCompiler");
Create let backup = msg.clone();
Print "Primary: ZenCompiler | Backup: ZenCompiler".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.