thecodex.expert · The Codex Family of Knowledge
Track 3 — Advanced

Rust: Advanced

Five lessons: lifetimes, smart pointers, concurrency, async, and unsafe.

🕐 ~5 hours5 lessonsRust 2024 edition

Lessons

Lesson 1

Lifetimes and lifetime annotations

What lifetimes are, when annotations are needed, the 'a syntax, and lifetime elision rules.

Prerequisite: Practitioner track complete

What to look for:

  • Lifetimes prevent dangling references — the compiler verifies this
  • 'a in fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
  • Lifetime elision: most cases do not need explicit annotations
  • struct with reference fields always need lifetime annotations
🕑 60 min
Lesson 2

Smart pointers: Box, Rc, Arc

Box for heap allocation, Rc for reference counting, Arc for thread-safe sharing, RefCell.

Prerequisite: Lesson 1 of Advanced

What to look for:

  • Box: single owner, heap allocation
  • Rc: multiple owners, single thread
  • Arc: multiple owners, multiple threads (atomic reference count)
  • Rc> pattern for shared mutable state
🕑 50 min
Lesson 3

Threads, Mutex, and channels

thread::spawn, join handles, Arc> for shared state, mpsc channels, Send and Sync.

Prerequisite: Lesson 2 of Advanced

What to look for:

  • thread::spawn(move || ...) — move ownership into thread
  • Mutex lock guard is released when it goes out of scope (RAII)
  • mpsc::channel() — multiple producers, one consumer
  • Send and Sync marker traits — type system proof of thread safety
🕑 50 min
Lesson 4

Async Rust and Tokio basics

async fn, await, futures are lazy, Tokio runtime, tokio::spawn, and async I/O.

Prerequisite: Lesson 3 of Advanced

What to look for:

  • async fn returns a Future — nothing runs until awaited
  • Futures are lazy: calling an async fn does nothing alone
  • #[tokio::main] — Tokio provides the executor
  • tokio::join! for concurrent awaiting — like Promise.all
🕑 60 min
Lesson 5

unsafe and FFI

What unsafe unlocks, when to use it, calling C from Rust with extern "C", and the five unsafe superpowers.

Prerequisite: Lessons 1-4 of Advanced

What to look for:

  • unsafe enables 5 things only: raw pointers, unsafe fns, mutable statics, unsafe traits, extern
  • unsafe does NOT disable the borrow checker — it only unlocks these 5
  • FFI: extern "C" { fn strlen(s: *const i8) -> usize; }
  • Keep unsafe blocks small and well-documented
🕑 40 min

How to use this track: Each lesson links to the Codex Rust reference page. Use reading level tabs. Rust borrow checker errors are the lesson — read them carefully. Practice at play.rust-lang.org.

Track quiz

4 questions to check your understanding.

What do lifetime annotations ('a) tell the Rust compiler?

When would you use Arc instead of Rc?

Why are Futures in Rust described as "lazy"?

What does unsafe in Rust actually enable?

← Practitioner trackGo language →