Part 2: Rust Fundamentals
This part covers the core concepts that make Rust unique: ownership, borrowing, and lifetimes. Mastering these concepts is essential for writing idiomatic Rust code.
What You’ll Learn
- Variables, mutability, and data types
- Functions and control flow
- Rust’s ownership system
- References and borrowing
- Lifetimes
- Structs and enums
- Pattern matching
- Error handling
- Traits and generics
Chapters
- Variables and Types - let, mut, scalars, and compounds
- Functions - Syntax, parameters, and returns
- Ownership - Rust’s unique memory model
- Borrowing - References and the borrowing rules
- Lifetimes - Annotations and elision
- Structs - Custom data types
- Enums - Pattern matching with Option and Result
- Error Handling - Panic, Result, and the ? operator
- Traits - Shared behavior
- Generics - Type parameters and bounds
The Big Picture
graph TD
A[Variables] --> B[Ownership]
B --> C[Borrowing]
C --> D[Lifetimes]
E[Structs] --> F[Methods]
G[Enums] --> H[Pattern Matching]
F --> I[Traits]
H --> I
I --> J[Generics]
D --> K[Error Handling]
H --> K
Prerequisites
- Completed Part 1: Getting Started
- Rust installed and working
- Basic programming knowledge
Key Concepts Preview
Ownership
Every value has exactly one owner. When the owner goes out of scope, the value is dropped:
{
let s = String::from("hello"); // s owns the String
} // s goes out of scope, String is dropped
Borrowing
You can borrow references to values without taking ownership:
fn calculate_length(s: &String) -> usize {
s.len() // Use s, but don't own it
}
Lifetimes
Lifetimes ensure references are valid:
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
Don’t worry if these look confusing now. Each chapter explains these concepts step by step with plenty of examples.
Time Estimate
Plan for 4-6 hours to work through this part thoroughly. Take breaks between chapters to let the concepts sink in.
Next Steps
Start with Variables and Types to begin your Rust journey.