This comprehensive Rust learning setup will take you from beginner to intermediate level with hands-on examples and structured exercises.
Rust is a systems programming language focused on:
- Memory Safety: No null pointer dereferences, buffer overflows, or memory leaks
- Performance: Zero-cost abstractions, no garbage collector
- Concurrency: Safe concurrent programming
- Cross-platform: Runs on many platforms and architectures
rust_learning/
โโโ README.md # This file - complete learning guide
โโโ rust_cheatsheet.md # Quick syntax reference
โโโ exercises.md # 7 structured practice exercises
โ
โโโ examples/ # Working code examples (numbered in learning order)
โ โโโ 001-variables_and_data_types.rs # Variables, mutability, and all data types
โ โโโ 002-functions.rs # Function examples
โ โโโ 003-control_flow.rs # If/else, loops, and match
โ โโโ 004-ownership_borrowing.rs # Ownership and borrowing
โ โโโ 005-structs_enums.rs # Structs and enums with methods
โ โโโ 006-pattern_matching.rs # Advanced pattern matching
โ โโโ 007-error_handling.rs # Result and Option types
โ โโโ 008-collections.rs # Vec, HashMap, HashSet, and other collections
โ โโโ 009-generics-traits.rs # Generic types and trait system
โ โโโ 010-lifetimes.rs # Lifetime annotations and management
โ โโโ 011-modules-crates.rs # Module system and crate organization
โ โโโ 012-file-io.rs # File I/O operations and path handling
โ โโโ 013-testing.rs # Unit testing and test organization
โ โโโ 014-command-line.rs # Building command-line programs
โ โโโ 015-concurrency-threads.rs # Concurrency and Threads (channels, Mutex, Arc)
โ โโโ 016-async-programming.rs # Asynchronous Programming (async/await, Tokio)
โ โโโ 017-unsafe-rust.rs # Unsafe Rust (raw pointers, FFI)
โ โโโ 018-macros.rs # Macros (declarative, procedural)
โ โโโ 019-web-development.rs # Web Development (frameworks, HTTP, APIs)
โ โโโ 020-system-programming.rs # System Programming (OS interaction, low-level networking)
โ
โโโ exercises/ # Practice exercises directory
โ โโโ ex1_variables.rs # Exercise 1 (example)
โ
โโโ hello_rust/ # Your first Cargo project
โ โโโ Cargo.toml # Project configuration
โ โโโ src/
โ โ โโโ main.rs # "Hello, world!" program
โ โโโ target/ # Compiled output (auto-generated)
โ
โโโ guessing_game/ # Interactive number guessing game
โโโ Cargo.toml # Project configuration with dependencies
โโโ src/
โ โโโ main.rs # Complete guessing game code
โโโ target/ # Compiled output (auto-generated)
-
rust_cheatsheet.md- Quick reference for Rust syntax -
exercises.md- 7 structured exercises to practice -
examples/- Working code examples (numbered in learning order):Phase 1 (Basics):
001-variables_and_data_types.rs- Variables, mutability, and all data types002-functions.rs- Function examples003-control_flow.rs- If/else, loops, and match004-ownership_borrowing.rs- Ownership and borrowing005-structs_enums.rs- Structs and enums with methods006-pattern_matching.rs- Advanced pattern matching007-error_handling.rs- Result and Option types
Phase 2 (Intermediate):
008-collections.rs- Vec, HashMap, HashSet, and other collections009-generics-traits.rs- Generic types and trait system010-lifetimes.rs- Lifetime annotations and management011-modules-crates.rs- Module system and crate organization012-file-io.rs- File I/O operations and path handling013-testing.rs- Unit testing and test organization014-command-line.rs- Building command-line programs
Phase 3 (Advanced):
015-concurrency-threads.rs- Concurrency and Threads (channels, Mutex, Arc)016-async-programming.rs- Asynchronous Programming (async/await, Tokio)017-unsafe-rust.rs- Unsafe Rust (raw pointers, FFI)018-macros.rs- Macros (declarative, procedural)019-web-development.rs- Web Development (frameworks, HTTP, APIs)020-system-programming.rs- System Programming (OS interaction, low-level networking)
-
exercises/- Practice exercises directory with example solutions -
hello_rust/- Your first Cargo project -
guessing_game/- Interactive number guessing game (ready to play!)
-
Try the guessing game right now:
cd guessing_game && cargo run
-
Run the examples (in order):
# Phase 1 (Basics) rustc examples/001-variables_and_data_types.rs -o 001-variables_and_data_types && ./001-variables_and_data_types rustc examples/002-functions.rs -o 002-functions && ./002-functions rustc examples/003-control_flow.rs -o 003-control_flow && ./003-control_flow # Phase 2 (Intermediate) - Try after completing Phase 1 rustc examples/008-collections.rs -o 008-collections && ./008-collections rustc examples/009-generics-traits.rs -o 009-generics-traits && ./009-generics-traits
-
Start with Exercise 1 - Create your first program following the exercises.md
-
Use the cheatsheet - Keep
rust_cheatsheet.mdopen as reference
Phase 1 (Basics):
- Ownership (most unique/important concept)
- Borrowing and references (&)
- Pattern matching with
match - Error handling with
ResultandOption
Phase 2 (Intermediate):
- Collections for data storage (Vec, HashMap)
- Generics and Traits for code reusability
- Lifetimes for advanced memory management
- Modules for code organization
- Testing for code reliability
- Read compiler error messages carefully - Rust's compiler is very helpful!
- Use
cargo checkfor faster error checking - Don't worry about lifetimes initially - focus on ownership first
- Practice daily, even just 15 minutes
- Variables and Data Types โ (mutability, constants, shadowing, scalar/compound types)
- Functions โ (parameters, return values, expressions vs statements)
- Control Flow โ (if/else, loops, match expressions, nested patterns)
- Ownership and Borrowing โ (move semantics, references, lifetimes, string slices)
- Structs and Enums โ (methods, associated functions, complex enum variants)
- Pattern Matching โ (match guards, destructuring, if let/while let)
- Error Handling โ (Result/Option types, ? operator, custom errors)
- Collections โ (Vec, HashMap, HashSet, VecDeque, BTreeMap, iterator patterns)
- Generics and Traits โ (generic functions/structs, trait bounds, associated types)
- Lifetimes โ (lifetime annotations, elision rules, static lifetimes)
- Modules and Crates โ (module system, privacy, use statements, external crates)
- File I/O โ (reading/writing files, buffered I/O, path operations)
- Testing โ (unit tests, integration tests, assertions, test organization)
- Command Line Programs โ (args, environment variables, stdin/stdout, CLI tools)
- Concurrency and Threads ๐ง (channels, Mutex, Arc)
- Async Programming ๐ง (async/await, Tokio, Futures)
- Unsafe Rust ๐ง (raw pointers, FFI,
unsafekeyword) - Macros ๐ง (declarative, procedural, custom macros)
- Web Development ๐ง (frameworks like Actix Web/Axum, HTTP, APIs)
- System Programming ๐ง (OS interaction, low-level networking, embedded)
# Create a new project
cargo new project_name
# Build the project
cargo build
# Run the project
cargo run
# Check for errors without building
cargo check
# Run tests
cargo test
# Build optimized version
cargo build --releaseGet started immediately with these commands:
# Play the guessing game
cd guessing_game && cargo run
# Try the examples (in order)
# Phase 1 examples
rustc examples/001-variables_and_data_types.rs -o 001-variables_and_data_types && ./001-variables_and_data_types
rustc examples/002-functions.rs -o 002-functions && ./002-functions
rustc examples/003-control_flow.rs -o 003-control_flow && ./003-control_flow
# Phase 2 examples (intermediate)
rustc examples/008-collections.rs -o 008-collections && ./008-collections
rustc examples/009-generics-traits.rs -o 009-generics-traits && ./009-generics-traits
rustc examples/010-lifetimes.rs -o 010-lifetimes && ./010-lifetimes
# Try the first exercise
rustc exercises/ex1_variables.rs -o ex1_variables && ./ex1_variables
# Then complete more exercises following exercises.md- Read error messages carefully - they're very helpful!
- Use
cargo checkfor faster error checking - Don't fight the borrow checker - learn from it
- Use
cargo fmtto format your code - Use
cargo clippyfor additional lints
// Using Result
fn divide(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err("Division by zero".to_string())
} else {
Ok(a / b)
}
}
// Using Option
fn find_word(text: &str, word: &str) -> Option<usize> {
text.find(word)
}let numbers = vec![1, 2, 3, 4, 5];
// Filter and collect
let evens: Vec<i32> = numbers
.iter()
.filter(|&x| x % 2 == 0)
.copied()
.collect();
// Map and sum
let sum: i32 = numbers
.iter()
.map(|x| x * x)
.sum();- Temperature converter
- Simple calculator
- Word counter
- File reader
- Basic CLI tools
- Web server with Actix or Axum
- Database integration
- JSON API
- Command-line application with clap
- Simple game
- Async web crawler
- Database implementation
- Compiler or interpreter
- Network protocol implementation
- Performance-critical system tools
- The Rust Programming Language Book - The official book
- Rust by Example - Learn by examples
- Rustlings - Small exercises
- Rust Playground - Online Rust compiler
- The Cargo Book - Package manager guide
You're all set to start your Rust journey! The language has a learning curve, but it's incredibly rewarding. Start with the guessing game, then work through the examples and exercises.
Happy coding! ๐ฆ