Rust Demo Examples
Practice programs, one file each. Every demo is self-contained, commented, and uses only the standard library — so nothing needs downloading: compile with
rustc and run.
How to use: click View to open a file in the code viewer, read its comments, download it, then run it on your machine. Most demos are plain binaries; the tests demo (#20) needs --test.
rustc 01_hello.rs && ./01_hello # compile and run a demo
rustc --test 20_testing.rs -o 20_testing # compile unit tests
./20_testing
Foundations
Hello world, variables, scalar types, and strings — PHASE 1.
| # | Description | Link |
|---|---|---|
| 1 | Hello, World | View |
| 2 | Variables & Shadowing | View |
| 3 | Scalar Types & Casting | View |
| 4 | Strings & Formatting | View |
Ownership & Borrowing
Moves, borrows, and lifetimes — the core of Rust.
| # | Description | Link |
|---|---|---|
| 5 | Move Semantics & Copy | View |
| 6 | Shared & Mutable Borrowing | View |
| 7 | Lifetime Annotations | View |
Functions & Collections
Functions, closures, and the standard collections.
| # | Description | Link |
|---|---|---|
| 8 | Functions & Return Values | View |
| 9 | Closures | View |
| 10 | Vec, HashMap, HashSet | View |
Control & Errors
Conditionals, loops, and idiomatic error handling.
| # | Description | Link |
|---|---|---|
| 11 | if/else and match | View |
| 12 | loop, while, for | View |
| 13 | Option, Result, and ? | View |
Generics & Traits
Generic functions, structs, and trait-based polymorphism.
| # | Description | Link |
|---|---|---|
| 14 | Generics | View |
| 15 | Traits & Trait Objects | View |
Iterators & Smart Pointers
Lazy pipelines and the ownership helpers.
| # | Description | Link |
|---|---|---|
| 16 | Iterator Combinators | View |
| 17 | Box, Rc, RefCell | View |
Professional
Structs & methods, threads, and unit tests.
| # | Description | Link |
|---|---|---|
| 18 | Structs & Methods | View |
| 19 | Threads & Channels | View |
| 20 | Unit Tests (--test) | View |
Practice move: after running each demo, break it on purpose — move a value twice, borrow mutably while a shared borrow lives, or index past the end — and read each compiler error before fixing it.