Tutorial
Rust Tutorial
A practical tutorial to get you productive with Rust for building reliable and efficient software. Covers ownership, traits, error handling, concurrency, and the Cargo ecosystem.
Chapters
01
Python to Rust: A Transition Guide
02
Introduction to Rust
03
Basics: Variables, Types, and Operations
04
Ownership: Rust's Superpower
05
Control Flow
06
Functions
07
Structs and Enums
08
Collections
09
Error Handling
10
Traits and Generics
11
Modules and Crates
12
Concurrency
13
Cargo and the Rust Ecosystem
14
Best Practices and Idiomatic Rust
About this tutorial
A practical tutorial to get you productive with Rust, the language for building reliable and efficient software.
Who This Is For
- Developers with experience in other languages
- Python developers wanting performance without sacrificing safety
- Those wanting to learn systems programming with memory safety
- Anyone interested in concurrent programming without data races
- Developers building CLI tools, web services, or performance-critical applications
Contents
For Python Developers
- Python to Rust - Quick comparison guide for Python developers (start here!)
Fundamentals
- Introduction - What is Rust, installation, first program
- Basics - Variables, types, operators, strings
- Ownership - Ownership, borrowing, references, lifetimes
- Control Flow - If, loops, pattern matching
- Functions - Functions, closures, methods
Core Concepts
- Structs & Enums - Structs, enums, pattern matching
- Collections - Vec, HashMap, String
- Error Handling - Result, Option, error propagation
Advanced Features
- Traits - Traits, generics, trait bounds
- Modules & Crates - Module system, packages, crates
- Concurrency - Threads, channels, Arc, Mutex
Ecosystem & Mastery
- Cargo & Ecosystem - Cargo, testing, documentation
- Best Practices - Idioms, patterns, common mistakes
How to Use This Tutorial
- Read sequentially for a complete learning path
- Type out all examples - Rust's compiler is your teacher
- Experiment - change the code and see what breaks
- Build small projects after each section
Quick Reference
Essential Commands
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Create a new project
cargo new myproject
cd myproject
# Run a program
cargo run
# Build for release
cargo build --release
# Run tests
cargo test
# Check code without building
cargo check
# Format code
cargo fmt
# Run linter
cargo clippy
Hello World
fn main() {
println!("Hello, World!");
}
Common Patterns
// Error handling with Result
fn read_file(path: &str) -> Result<String, std::io::Error> {
std::fs::read_to_string(path)
}
// Option handling
let maybe_value: Option<i32> = Some(42);
if let Some(value) = maybe_value {
println!("Got: {}", value);
}
// Pattern matching
match result {
Ok(value) => println!("Success: {}", value),
Err(e) => eprintln!("Error: {}", e),
}
// Iterators
let sum: i32 = vec![1, 2, 3, 4, 5]
.iter()
.map(|x| x * 2)
.sum();
Learning Path Suggestions
Python Developer (2-3 weeks)
- Start with Chapter 0 - Python to Rust comparison
- Fundamentals (chapters 1-5) - Focus on ownership differences
- Error Handling (8) - Replace exceptions with Result/Option
- Collections (7) - HashMap, Vec vs dict, list
- Traits (9) - Similar to protocols/interfaces
- Build a CLI tool or REST API
Systems Programmer (1-2 weeks)
- All fundamentals (chapters 1-5)
- Focus deeply on Ownership (3)
- Structs, Enums, Traits (6, 9)
- Concurrency (11)
- Build a CLI tool or file processor
Web Developer (1-2 weeks)
- Fundamentals (chapters 1-5)
- Error Handling (8)
- Traits (9)
- Modules & Crates (10)
- Learn async Rust (not in this tutorial)
- Build a REST API with actix-web or axum
General Purpose Programmer (2-3 weeks)
- Complete all chapters in order
- Focus on understanding the compiler messages
- Build progressively larger projects
- Read other people's Rust code
Why Rust?
- Memory Safety: No null pointer dereferences, no data races
- Performance: Zero-cost abstractions, comparable to C/C++
- Concurrency: Fearless concurrency without data races
- Tooling: Excellent package manager (Cargo), formatter, linter
- Community: Helpful, welcoming, thorough documentation
Additional Resources
- The Rust Book - Official comprehensive guide
- Rust by Example - Learn by doing
- Rustlings - Small exercises
- Rust Playground - Try Rust in the browser
- Docs.rs - Documentation for all crates
- Crates.io - Rust package registry
Rust Version
This tutorial is written for Rust 2021 edition (Rust 1.56+) and updated for Rust 1.93. The examples work with both the 2021 and 2024 editions.