A comprehensive collection of Rust examples covering fundamental concepts to advanced patterns. This repository has been modernized to use Rust 2021 edition with current best practices and idioms.
- Rust: Version 1.75 or later (2021 edition)
- Install via rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Navigate to any example directory and run:
cd <example-folder>
cargo runTo build without running:
cargo buildThis repository is organized as a progressive learning curriculum. Follow the sections in order for the best learning experience.
Your first Rust program using Cargo.
- Concepts: Project structure, Cargo basics
- Key Files:
Cargo.toml,src/main.rs - Run:
cd hello_cargo && cargo run
Understanding constants and their differences from variables.
- Concepts:
constvslet, type annotations, numeric separators - Learn: Constants must have explicit types and are immutable
- Example:
const MAX_POINTS: u32 = 100_000;
Comprehensive overview of Rust's type system.
- Concepts: Integers (u8, i32, u64, etc.), floats, characters, strings
- Learn: Type inference, explicit type annotations, literals
- Example: Parsing strings to numbers, working with different numeric types
Introduction to mutability in Rust.
- Concepts:
mutkeyword, mutable references - Learn: Variables are immutable by default
- Example: Modifying values through mutable references
Fixed-size arrays vs dynamic vectors.
- Concepts: Arrays
[T; N], VectorsVec<T> - Learn: Arrays are stack-allocated, vectors grow dynamically
- Example: Array initialization, vector operations (push, indexing)
Passing data between functions: by value, reference, or mutable reference.
- Concepts: Borrowing, ownership transfer,
&strvs&String - Learn: Ownership moves, borrowing rules
- Example: Functions that borrow vs consume values
Error handling and optional values the Rust way.
- Concepts:
Option<T>,Result<T, E>, pattern matching - Learn:
match,if let,unwrap(),unwrap_or() - Example: Handling
Nonecases, error propagation with?
Defining types and behaviors with structs and traits.
- Concepts: Structs, traits (interfaces), implementations
- Learn: Methods vs associated functions, trait bounds
- Example:
Persontrait implemented forManstruct
Implementing standard library traits for custom types.
- Concepts: Operator overloading with
AddAssign,DivAssign - Learn: How to make custom types work with operators like
+= - Example: Custom
Counttype with arithmetic operations
Understanding the dereference operator and smart pointers.
- Concepts:
Deref,DerefMuttraits, the*operator - Learn: Deref coercion, mutable dereferencing
- Example: Creating smart pointer-like types
Design patterns in Rust: the Visitor pattern.
- Concepts: Design patterns, trait objects (
dyn Trait) - Learn: Separating data structures from operations
- Example: Animal structs with pluggable sound behaviors
Closures as function arguments with generic trait bounds.
- Concepts: Closures,
Fntrait, generics, caching/memoization - Learn: Using closures for expensive operations, result caching
- Example: Search cacher with closure-based file operations
Declarative macros for code generation.
- Concepts:
macro_rules!, repetition patterns, compile-time code generation - Learn: When and how to write macros
- Example: Cashier calculator macro with variable arguments
RESTful API using Rocket web framework (v0.5, async/await).
- Concepts: HTTP routes, JSON serialization, async handlers
- Learn: Building REST APIs with Rocket
- Example: CRUD operations for parking management
- Run:
cd parking-api && cargo run, then visithttp://127.0.0.1:8000
GraphQL API with MongoDB database integration (modern async).
- Concepts: GraphQL, async/await, database operations, error handling
- Learn: Modern async Rust, MongoDB driver, Juniper GraphQL
- Dependencies: MongoDB running on
localhost:27017 - Example: Product CRUD via GraphQL
- Run:
cd graphql-juniper-mongo && cargo run, then visithttp://127.0.0.1:3030
| Concept | Examples |
|---|---|
| Ownership & Borrowing | mutations-functions, basic-mutations |
| Error Handling | match-some-err-options-results |
| Traits & Generics | trait-struct-impl, closures-into-traits |
| Pattern Matching | match-some-err-options-results, visitor-pattern |
| Macros | cashier-macro |
| Async Programming | parking-api, graphql-juniper-mongo |
| Web Development | parking-api, graphql-juniper-mongo |
- The Rust Book: doc.rust-lang.org/book
- Rust by Example: doc.rust-lang.org/rust-by-example
- Rust Standard Library: doc.rust-lang.org/std
- Cargo Book: doc.rust-lang.org/cargo
This repository was originally created in 2019 (Rust 2018 edition) and has been modernized:
- β Updated to Rust 2021 edition
- β
Removed deprecated
extern cratedeclarations - β Applied modern idioms and best practices
- β Updated dependencies to latest stable versions
- β
Replaced
failurewithanyhow/thiserror - β Modernized web frameworks (Rocket 0.5, async MongoDB driver)
- β Added comprehensive documentation
Feel free to open issues or pull requests to improve examples or add new ones!
Educational purposes - feel free to use and modify.
Happy Learning! π¦