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: One Rule, Three Corollaries
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 that gets you productive with Rust: the language for building reliable, efficient software.
Who This Is For
- Developers with experience in another language
- Python developers who want performance without losing safety
- Anyone learning systems programming with memory safety
- Developers writing concurrent code that should be free of data races
- Builders of CLI tools, web services, and performance-critical applications
Contents
For Python Developers
- Python to Rust: Side-by-side comparison guide (start here)
Fundamentals
- Introduction: What Rust is, 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 and Enums: Structs, enums, pattern matching
- Collections:
Vec,HashMap,String - Error Handling:
Result,Option, error propagation
Advanced Features
- Traits: Traits, generics, trait bounds
- Modules and Crates: Module system, packages, crates
- Concurrency: Threads, channels,
Arc,Mutex
Ecosystem and Mastery
- Cargo and Ecosystem: Cargo, testing, documentation
- Best Practices: Idioms, patterns, common mistakes
How to Use This Tutorial
- Read sequentially for a complete learning path
- Type out the examples. The compiler is your teacher
- Experiment: change the code and see what the compiler says
- 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 to 3 weeks)
- Start with Chapter 0: Python to Rust comparison
- Fundamentals (chapters 1 to 5): focus on ownership differences
- Error Handling (8): replace exceptions with
Result/Option - Collections (7):
HashMap,Vecvs dict, list - Traits (9): close to protocols/interfaces
- Build a CLI tool or REST API
Systems Programmer (1 to 2 weeks)
- All fundamentals (chapters 1 to 5)
- Spend extra time on Ownership (3)
- Structs, Enums, Traits (6, 9)
- Concurrency (11)
- Build a CLI tool or file processor
Web Developer (1 to 2 weeks)
- Fundamentals (chapters 1 to 5)
- Error Handling (8)
- Traits (9)
- Modules and Crates (10)
- Learn async Rust (covered elsewhere)
- Build a REST API with
actix-weboraxum
General Purpose Programmer (2 to 3 weeks)
- Read every chapter in order
- Focus on 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: Compiler-checked thread safety, no data races
- Tooling: Cargo, rustfmt, clippy, all in the box
- Community: Welcoming, with thorough documentation
Additional Resources
- The Rust Book: the official comprehensive guide
- Rust by Example: learn by doing
- Rustlings: small exercises
- Rust Playground: try Rust in the browser
- Docs.rs: documentation for all published 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.