Ownership Basics
Understand the core concept that makes Rust unique: Ownership. Learn how Rust manages memory without a garbage collector.
Learning Objectives
- Understand the three rules of Ownership
- Learn about Variable Scope
- Understand the difference between Copy and Move
- Introduction to the Stack vs. the Heap
What is Ownership?
Ownership is Rust’s most unique feature, and it enables Rust to make memory safety guarantees without needing a garbage collector.
The Stack vs. The Heap
To understand ownership, you first need to know how Rust uses memory:
- The Stack: Fast, fixed-size data (like integers, booleans, and characters).
- The Heap: Slower, but can hold data that grows in size (like
StringorVec).
The Three Rules of Ownership
- Each value in Rust has a variable that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped (removed from memory).
Variable Scope
A scope is the range within a program for which an item is valid.
|
|
Data Interaction: Move
When we assign one variable to another, Rust does something different depending on the data type.
Simple Types (Copy)
For types with a fixed size (on the Stack), Rust copies the value.
|
|
Complex Types (Move)
For types on the Heap (like String), Rust moves the ownership. The first variable is no longer valid!
|
|
Why? To prevent “double free” errors. If both variables owned the same memory, they would both try to clean it up when they go out of scope, causing a crash.
Ownership and Functions
Passing a value to a function will move or copy it, just as assignment does.
|
|
Summary
Today you learned:
- The 3 Rules of Ownership.
- Fixed-size data is Copied (Stack).
- Dynamic-size data is Moved (Heap).
- Ownership prevents memory leaks and double-free bugs without a garbage collector.
Practice Exercise
- Create a
Stringand try to pass it to two different functions sequentially. What happens? - How can you use a
Stringagain after passing it to a function? (Hint: The function could return it back, but we’ll learn a better way—Borrowing—next week!) - Use the
.clone()method to manually copy aStringinstead of moving it.
Next Steps
Tomorrow, we’ll wrap up Week 1 with a Review and Practice Project!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 6 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!