Welcome to the definitive collection of Rust code snippets! This repository contains 800 professional, commented, and verified Rust examples, meticulously organized to take you from a complete beginner to an advanced systems engineer.
π’ Basic Level (250 Snippets)
Core fundamentals of the Rust programming language.
- 01 Introduction: Syntax, variables, hello world, and environment.
- 02 Types: Scalars, compounds, casting, and memory representation.
- 03 Control Flow: Branching, looping, matching, and expressions.
- 04 Functions: Signatures, recursion, closures, and pointers.
- 05 Ownership: Move, clone, borrow checker, and lifetimes intro.
- 06 Structs & Enums: Data modeling, methods, and Option/Result.
- 07 Collections: Vectors, HashMaps, Sets, and iteration patterns.
- 08 Error Handling: Panic, Result, propagation, and custom types.
- 09 Std Lib Macros:
format!,vec!,assert!, and diagnostic macros. - 10 Iterators Depth: Mastering adapter chains and custom implementations.
- 11 Common Algorithms: Sorting, searching, and fundamental logic.
- 12 Debugging: Logging, testing, linting, and troubleshooting.
- 13 Functional Basics: Mapping, filtering, and lazy evaluation.
- 14 Error Patterns: Idiomatic failure models and recovery strategies.
- 15 Std Library Types: Primitives, buffers, and system-level types.
π‘ Intermediate Level (300 Snippets)
Idiomatic patterns and practical development skills.
- 01 Generics & Traits: Polymorphism, trait bounds, and dispatch.
- 02 Lifetimes: Explicit annotations, variance, and subtyping.
- 03 Closures & Iterators: Functional patterns and performance.
- 04 Smart Pointers: Box, Rc, RefCell, Arc, and Cow internals.
- 05 Concurrency: Multi-threading, channels, and synchronization.
- 06 Modules & Testing: Organization, unit tests, and benchmarks.
- 07 I/O & Networking: Files, TCP/UDP, and environment interaction.
- 08 Design Patterns: Classic software patterns in idiomatic Rust.
- 09 Procedural Logic: State management and data flow pipelines.
- 10 Web Server Basics: HTTP foundations and request processing.
- 11 Database Concepts: Connection pools, SQL, and data modeling.
- 12 CLI Development: Argument parsing, terminal UI, and UX.
- 13 Advanced Collections: Specialized containers and complexity analysis.
- 14 Concurrency Patterns: Worker pools, lock-free, and async patterns.
- 15 Serialization Depth: Serde mastery and data transformation.
π΄ Advanced Level (250 Snippets)
Systems programming, performance, and low-level mastery.
- 01 Advanced Lifetimes: HRTBs, invariance, and leak analysis.
- 02 Advanced Traits: GATs, specialization, and coherence rules.
- 03 Macros: Metaprogramming with rules and proc-macros.
- 04 Unsafe Rust: Raw pointers, FFI, UB boundaries, and pinning.
- 05 Async/Await: Future polling, wakers, and executor design.
- 06 Memory Management: Layout, manual allocation, and repr rules.
- 07 Metaprogramming: Const generics and type-level logic.
- 08 Profiling & Performance: Micro-benchmarking and optimization.
- 09 Custom Allocators: Arena, bump, and slab implementation.
- 10 Panic Runtime: Unwinding mechanics and custom handlers.
- 11 Type-Level Mastery: Peano arithmetic and session types.
- 12 Graphics & WASM: WebAssembly, GPU shaders, and rendering.
- 13 Low-Level Opts: SIMD, bit-hacks, and monomorphization.
- 14 Memory Safety: Miri, stacked borrows, and formal analysis.
- 15 Embedded Rust: no_std foundations, HAL, and bare-metal.
Every snippet in this collection is a standalone, valid Rust file (targeting the Rust 2021 Edition). You can verify any snippet by running:
rustc --edition 2021 --crate-type lib <path_to_snippet>.rs- Idiomatic: Each snippet follows "The Rust Way" of doing things.
- Commented: Professional comments explain the logic and the "why" behind the code.
- Verified: All 800 snippets have been automatically verified for syntax correctness.
- Educational: Designed to be read and understood by developers looking to improve their Rust skills.
This collection is maintained for the Rust community. Happy coding! π¦