Learning Rust Programming
Master safe systems programming with Rust's revolutionary ownership model. Build blazingly fast, memory-safe applications without garbage collection.
Why Learn Rust?
- Memory Safety Without GC: Rust eliminates entire classes of bugs at compile time through its ownership system
- Blazingly Fast: Zero-cost abstractions mean Rust is as fast as C/C++ while being safer
- Fearless Concurrency: Write parallel code without data races - the compiler catches threading bugs
- Modern Tooling: Cargo makes dependency management, testing, and building a joy
- Growing Ecosystem: Used by Mozilla, Dropbox, AWS, Microsoft, and more for performance-critical systems
Your Learning Path
Master Rust's unique concepts step-by-step:
2-3 hours
4-5 hours
2-3 hours
2-3 hours
2-3 hours
3-4 hours
3-4 hours
Interactive Lessons
Click each lesson to expand and learn.
Variables, Types & Mutability
Rust variables are immutable by default - you must explicitly use mut to make them mutable.
fn main() {
let x = 5; // Immutable by default
// x = 6; // ERROR: cannot assign twice
let mut y = 10; // Explicitly mutable
y = 15; // OK
// Type annotations (usually inferred)
let z: i32 = 42;
let pi: f64 = 3.14159;
let name: &str = "Alice";
// Shadowing (different from mutation)
let spaces = " ";
let spaces = spaces.len(); // New variable, different type
}
Ownership & Borrowing
Rust's ownership system is its most unique feature. Every value has a single owner, and borrowing follows strict rules.
fn main() {
let s1 = String::from("hello");
let s2 = s1; // s1 is MOVED to s2, s1 is invalid
// println!("{}", s1); // ERROR: value borrowed after move
let s3 = String::from("world");
let len = calculate_length(&s3); // Borrow with &
println!("{} has length {}", s3, len); // s3 still valid
let mut s4 = String::from("hello");
change(&mut s4); // Mutable borrow
println!("{}", s4);
}
fn calculate_length(s: &String) -> usize {
s.len() // Returns length without taking ownership
}
fn change(s: &mut String) {
s.push_str(", world");
}
Structs & Methods
Structs group related data together. Methods are defined in impl blocks.
struct User {
username: String,
email: String,
active: bool,
}
impl User {
fn new(username: String, email: String) -> User {
User {
username,
email,
active: true,
}
}
fn deactivate(&mut self) {
self.active = false;
}
fn is_admin(&self) -> bool {
self.username == "admin"
}
}
Enums & Pattern Matching
Enums are incredibly powerful in Rust. Pattern matching with match is exhaustive and safe.
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn process_message(msg: Message) {
match msg {
Message::Quit => println!("Quit"),
Message::Move { x, y } => println!("Move to ({}, {})", x, y),
Message::Write(text) => println!("Write: {}", text),
Message::ChangeColor(r, g, b) => println!("Color: ({}, {}, {})", r, g, b),
}
}
Error Handling (Result & Option)
Rust doesn't have exceptions. Instead, use Result for errors and Option for nullable values.
use std::fs::File;
use std::io::Read;
fn read_file(path: &str) -> Result {
let mut file = File::open(path)?; // ? propagates errors
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn divide(a: f64, b: f64) -> Option {
if b == 0.0 {
None
} else {
Some(a / b)
}
}
Traits & Generics
Traits define shared behavior. Generics let you write flexible, reusable code.
trait Summary {
fn summarize(&self) -> String;
}
struct Article {
title: String,
content: String,
}
impl Summary for Article {
fn summarize(&self) -> String {
format!("{}: {}", self.title, &self.content[0..50])
}
}
fn notify(item: &T) {
println!("Breaking news! {}", item.summarize());
}
Async Programming with Tokio
Rust's async/await syntax enables efficient concurrent programming. Tokio is the most popular async runtime.
use tokio;
#[tokio::main]
async fn main() {
let result = fetch_data().await;
println!("Result: {}", result);
}
async fn fetch_data() -> String {
// Async operations don't block the thread
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
"Data fetched!".to_string()
}
Curated GitHub Repositories
Hand-picked repositories to accelerate your Rust learning.
Practice Projects
-
1. Command-Line Todo App
Build a CLI app using clap for argument parsing and serde for JSON serialization.
Key Concepts: ownership, structs, Result, file I/O, crates -
2. Multi-threaded Web Scraper
Scrape websites concurrently using reqwest and tokio for async networking.
Key Concepts: async/await, concurrency, error handling, HTTP requests -
3. Memory-Safe Linked List
Implement a doubly-linked list using Rc and RefCell to understand smart pointers.
Key Concepts: ownership, borrowing, smart pointers, unsafe code -
4. REST API with Actix-Web
Build a production-ready REST API with authentication, database, and error handling.
Key Concepts: web frameworks, async I/O, middleware, diesel ORM -
5. WebAssembly Game with Bevy
Create a 2D game using Bevy ECS framework and compile to WebAssembly.
Key Concepts: ECS architecture, WASM, game loops, resource management
Resources Hub
Official Docs
Tools
Practice
Community
Your Progress
Check off lessons as you complete them. Your progress is saved locally in your browser.