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.

#DescriptionLink
1Hello, WorldView
2Variables & ShadowingView
3Scalar Types & CastingView
4Strings & FormattingView

Ownership & Borrowing

Moves, borrows, and lifetimes — the core of Rust.

#DescriptionLink
5Move Semantics & CopyView
6Shared & Mutable BorrowingView
7Lifetime AnnotationsView

Functions & Collections

Functions, closures, and the standard collections.

#DescriptionLink
8Functions & Return ValuesView
9ClosuresView
10Vec, HashMap, HashSetView

Control & Errors

Conditionals, loops, and idiomatic error handling.

#DescriptionLink
11if/else and matchView
12loop, while, forView
13Option, Result, and ?View

Generics & Traits

Generic functions, structs, and trait-based polymorphism.

#DescriptionLink
14GenericsView
15Traits & Trait ObjectsView

Iterators & Smart Pointers

Lazy pipelines and the ownership helpers.

#DescriptionLink
16Iterator CombinatorsView
17Box, Rc, RefCellView

Professional

Structs & methods, threads, and unit tests.

#DescriptionLink
18Structs & MethodsView
19Threads & ChannelsView
20Unit 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.