RRust By Example

Rust Examples

78 practical Rust code examples organized by topic. Each example includes runnable code, explanations, and key concepts.

beginner30
intermediate30
advanced18

Basics

Variables, types, structs, enums, and fundamental Rust syntax.

Ownership

Ownership, borrowing, references, and move semantics.

Compound Types

Arrays, slices, tuples, strings, and Vec.

Flow Control

if/else, loops, match, and pattern matching.

Functions

Functions, closures, and higher-order functions.

Generics

Generic types, trait bounds, and type parameters.

Traits

Trait definition, implementation, and trait objects.

Error Handling

Result, Option, ? operator, and custom errors.

Collections

HashMap, HashSet, BTreeMap, and Vec operations.

Concurrency

Threads, channels, Arc, Mutex, and atomics.

Async

async/await, futures, and asynchronous programming.

Smart Pointers

Box, Rc, Cell, RefCell, and interior mutability.

Iterators

Iterator trait, adaptors, and lazy evaluation.

Patterns

Pattern matching, destructuring, and guards.

Modules

Modules, imports, and code organization.

Macros

Declarative macros and macro_rules!.

Unsafe

Unsafe code, raw pointers, and FFI.

Lifetimes

Lifetime annotations and lifetime elision.

Testing

Unit tests, integration tests, and test organization.

File I/O

Reading and writing files with std::fs.

Serialization

JSON and data serialization with serde.

CLI

Command line argument parsing and CLI tools.