Rust Tutorial with Examples.
Rust tutorial for beginners and beyond: memory-safe systems software, cloud infrastructure tooling and developer tools, without a garbage collector.
Beginner
Start here — no prior experience needed
Introduction to Rust
Learn what Rust is, why it matters, and where it excels — memory safety without a garbage collector.
Setting Up Rust
Install Rust with rustup, configure your editor, and write your first program.
Variables in Rust
Understand let, mut, shadowing, constants, and type inference in Rust.
Data Types in Rust
Explore Rust's scalar types, compound types (tuples and arrays), and type aliases.
Operators in Rust
Learn arithmetic, bitwise, comparison, and logical operators in Rust — with no implicit type coercion.
Control Flow in Rust
Master if/else expressions, loop, while, for, ranges, and breaking with values in Rust.
Functions in Rust
Learn fn syntax, return values, closures, and higher-order functions in Rust.
Ownership in Rust
Understand Rust's ownership model through runnable examples: the three rules, move semantics, the Copy trait, cloning, and Drop/RAII order.
Borrowing and References
Learn how Rust's borrow checker works — shared references, mutable references, and the rules that prevent data races.
Structs in Rust
Define Rust structs, implement methods and associated functions, use update syntax and #[derive], implement Display, and overload operators.
Enums in Rust
Learn Rust enums that carry data, Option<T> and Result<T, E>, exhaustive matching, and enums as state machines, with real compiler output.
Pattern Matching in Rust
Master Rust pattern matching: match, ranges, destructuring tuples, structs and nested enums, guards, @ bindings, if let, while let, slice patterns.
Collections in Rust
Work with Rust collections — Vec, HashMap, HashSet, BTreeMap and VecDeque — including the entry API and why HashMap iteration order changes.
Testing in Rust
Write unit tests, integration tests, and doc tests in Rust using cargo test.
File I/O in Rust
Read and write files in Rust: fs helpers, buffered I/O, paths, directories, JSON with serde, environment variables, arguments, and stdin.
Intermediate
For developers with core concepts down
Lifetimes in Rust
Understand Rust lifetimes: annotation syntax, elision rules, lifetimes in structs, 'static, and the real compiler errors lifetimes prevent.
Traits in Rust
Define and implement Rust traits: default methods, trait bounds, impl Trait vs dyn Trait, From/Into, operator overloading, supertraits, object safety.
Generics in Rust
Write generic Rust functions, structs and enums: trait bounds, where clauses, const generics, blanket impls, and monomorphization, with real output.
Error Handling in Rust
Handle errors in Rust with Result, the ? operator, custom error types, thiserror and anyhow — each example run for real, including the failures.
Iterators in Rust
Understand Rust iterators: the Iterator trait, lazy evaluation traced step by step, adapter chains, consuming methods, and custom iterators.
Closures in Rust
Learn Rust closures through runnable examples: Fn, FnMut and FnOnce, environment capture, move closures, returning closures, and caching.
Concurrency in Rust
Write safe concurrent Rust with threads, Arc<Mutex<T>>, mpsc channels, RwLock, atomics, Send/Sync and Rayon, with real run output for each.
Smart Pointers in Rust
Understand Rust smart pointers — Box, Rc, Arc, RefCell and Cell — with real output, including the RefCell double-borrow panic and Rc's Send error.
Modules in Rust
Organize code with mod, pub, use, crate structure, and Cargo workspaces.
Advanced
Production-grade patterns for experienced engineers
Async/Await in Rust
Learn Rust async/await with Tokio: lazy futures, join!, spawn, select!, timeouts, streams, and an echo server, each with real cargo run output.
Memory Management in Rust
Understand Rust memory: Box, Rc, RefCell, Arc and Cell, measured type sizes and the null-pointer niche, plus the five things unsafe Rust permits.
Macros in Rust
Write Rust macros with macro_rules!, build derive and attribute procedural macros, and use serde, thiserror and clap — all compiled and run for real.
Design Patterns in Rust
Implement idiomatic Rust design patterns — builder, newtype, typestate, RAII guards, strategy closures and observers — as complete runnable programs.
Performance in Rust
Measure and optimize Rust: criterion benchmarks, debug vs release timings, allocation avoidance, struct layout, pre-allocation, Rayon, and const fn.
Rust Interview Prep
Top 30 Rust interview questions and answers covering ownership, traits, concurrency, async and system design, with runnable code for key answers.