Free tutorial · 29 lessons

Rust Tutorial

Learn Rust step by step: variables, types, functions and control flow, ownership, borrowing and lifetimes, structs, enums and pattern matching, collections and iterators, Result and error handling, traits and generics, smart pointers, threads, Arc and Mutex, async, modules and testing. Every example is compiled and run.

Start learning →

Rust Basics

  1. Rust Introduction — What Rust is, why developers and companies choose it, and what a Rust program looks like.
  2. Rust Get Started — Install Rust with rustup, create a project with Cargo, and build and run it.
  3. Rust Syntax and Printing — Statements, expressions, comments, and formatted output with println! and format!.
  4. Rust Variables and Mutability — Declare variables with let, make them mutable with mut, shadow them, and use constants.
  5. Rust Data Types — Integers, floats, booleans, characters, tuples and arrays, and how Rust handles type conversion and overflow.
  6. Rust Functions — Define functions with parameters and return types, and return values with expressions.
  7. Rust Control Flow — Branch with if and match, and repeat with loop, while and for over ranges and collections.
  8. Rust History & Versions — How Rust grew from a personal project at Mozilla into a language used in the Linux kernel, the 1.0 release and editions, the Rust Foundation, the six-week release train, and which version to use today.

Ownership and Borrowing

  1. Rust Ownership — The three ownership rules, moves, clones, and Copy types: how Rust manages memory without a garbage collector.
  2. Rust References and Borrowing — Borrow values with & and &mut instead of moving them, and the rules that prevent data races.
  3. Rust Slices and Strings — String versus &str, string slices, array slices, and working with UTF-8 text correctly.
  4. Rust Lifetimes — What lifetime annotations mean, when the compiler needs them, and how they prevent dangling references.

Structs, Enums and Collections

  1. Rust Structs and Methods — Group data in structs, add methods and associated functions with impl, and derive common traits.
  2. Rust Enums and Option — Model choices with enums that carry data, and handle missing values with Option instead of null.
  3. Rust Pattern Matching — Use match, if let, let else and let chains to destructure values and handle cases exhaustively.
  4. Rust Collections: Vec, HashMap and More — Store lists in Vec, key-value data in HashMap and BTreeMap, and unique values in HashSet.
  5. Rust Iterators and Closures — Process collections with iterator adapters like map, filter and sum, and write closures that capture their environment.

Error Handling

  1. Rust Result and the ? Operator — Return recoverable errors with Result, propagate them with ?, and handle them with match and combinators.
  2. Rust panic! and Custom Error Types — When to panic and when to return errors, and how to define error types that callers can match on.

Traits and Generics

  1. Rust Traits — Define shared behaviour with traits, implement them for your types, and use default methods.
  2. Rust Generics and Trait Bounds — Write functions and types that work with many types, constrained by trait bounds and where clauses.
  3. Rust Trait Objects and dyn — Store values of different types behind one interface with Box<dyn Trait>, and when to choose it over generics.

Smart Pointers and Concurrency

  1. Rust Smart Pointers: Box, Rc and RefCell — Put values on the heap with Box, share ownership with Rc, and mutate shared data with RefCell.
  2. Rust Threads and Channels — Run work in parallel with threads and scoped threads, and communicate between them with channels.
  3. Rust Shared State: Arc and Mutex — Share data between threads safely with Arc and Mutex, and how Send and Sync prevent data races at compile time.
  4. Rust Async and Await — What async Rust is for, how futures and runtimes like Tokio work, and when to use async or threads.

Projects and Testing

  1. Rust Modules, Crates and Cargo — Organise code into modules, control visibility with pub, and use external crates from crates.io.
  2. Rust Testing — Write unit tests with #[test] and assertions, test errors and panics, and run them with cargo test.
  3. Rust Next Steps — Where to go after this tutorial: the Rust book, building real projects, and related courses.

Was this page helpful?