Index of Examples
Contributing
Book Editing and Example Code Development
Dev Container and Docker
Development Environment Setup
Documentation Generation
mdBook Pre-Processors
Publication to crates.io
Repository Structure
| Folders and Key Files |
| Book Organization |
| Code Organization |
| All Examples are Fully and Continuously Tested |
Topics of Interest
Algorithms
Hashing
Random Value Generation
Sorting
| Recipe | Crates | Categories |
|---|---|---|
| Sort a Vector of Integers | ||
| Sort a Vector of Floats | ||
| Sort a Vector of Structs |
Data Structures
B-Trees
| Recipe | Crates | Categories |
|---|---|---|
Create a Map Sorted by Key with BTreeMap | ||
Create a Sorted Set with BTreeSet<T> |
Binary Heaps
| Recipe | Crates | Categories |
|---|---|---|
Implement a Priority Queue with priority-queue | ||
Implement a Priority Queue with priority-queue |
Bit Arrays
| Recipe | Crates | Categories |
|---|---|---|
Work with C-style Flags using bitflags | ||
Work with C-style Flags using flagset | ||
Create and Manipulate Bit Vectors with bitvec | ||
Store Large Sets of Integers with roaring |
Graphs
| Recipe | Crates | Categories |
|---|---|---|
Create and Manipulate Graphs with petgraph |
Hashmaps
| Recipe | Crates |
|---|---|
| Store Key-Value Pairs into a HashMap | |
| Store Unique Items in a HashSet | |
| Use a Custom Type as the Key of a HashMap | |
Use a Custom Hash Function with HashMap and HashSet |
Heapless Data Structures
| Recipe | Crates | Categories |
|---|---|---|
Work with Heapless Data Structures with heapless |
Linked Lists
| Recipe | Crates | Categories |
|---|---|---|
| Store Data in a Linked List |
Other Maps
| Recipe | Crates | Categories |
|---|---|---|
| Store Data in an Insertion-ordered Map | ||
Store Multiple Values for a Single Key with multimap | ||
| Store Collections of Objects that Need Unique, Stable, Safe Identifiers |
Stack-Allocated Arrays
Stacks and Queues
| Recipe | Crates | Categories |
|---|---|---|
Implement a Queue Using VecDeque | ||
Implement a Stack Using Vec |
Strings
| Recipe | Crates |
|---|---|
String and &str | |
| Print and Format Strings | |
| Concatenate Strings |
Unique Identifiers
| Recipe | Crates | Categories |
|---|---|---|
| Generate and Parse UUIDs | ||
| Generate and Parse ULIDs |
Vectors
Filesystem
Directories
| Recipe | Crates | Categories |
|---|---|---|
| Get the Current Working Directory | ||
| Create, List Contents of, and Remove Directories | ||
Remove a Directory and its Contents with remove_dir_all |
Directory Traversal
File Metadata
| Recipe | Crates | Categories |
|---|---|---|
| Display File Metadata |
File Watching
| Recipe | Crates | Categories |
|---|---|---|
| Watch Files or Directories and Execute a Function When They Change |
Paths
| Recipe | Crates | Categories |
|---|---|---|
| Canonicalize a Path | ||
Ensure your Paths are UTF-8 with camino | ||
| Create and Manipulate Paths |
Reading and Writing Files
| Recipe | Crates | Categories |
|---|---|---|
| Read from and Write to a File | ||
| Avoid Writing and Reading from the Same File | ||
| Access a File Randomly Using a Memory Map |
Symbolic Links
| Recipe | Crates | Categories |
|---|---|---|
| Work with Symbolic Links |
Temporary Files and Directories
| Recipe | Crates | Categories |
|---|---|---|
| Create Temporary Files or Temporary Directories |
User Directories and Preferred Applications
| Recipe | Crates | Categories |
|---|---|---|
Locate User Directories with dirs | ||
Locate User Directories with directories | ||
| Open a File or URL using the Program Configured on your System |
Working with The Standard Input and Output
| Recipe | Crates | Categories |
|---|---|---|
| Read from the Standard Input and Write to the Standard Output or Standard Error |
Mathematics
Additional Numeric Types
Complex Numbers
| Recipe | Crates | Categories |
|---|---|---|
| Create Complex Numbers | ||
| Add Complex Numbers | ||
| Use Mathematical Functions on Complex Numbers |
Linear Algebra
| Recipe | Crates | Categories |
|---|---|---|
| Calculate Vector Norms | ||
| Add Matrices | ||
| Multiply Matrices | ||
| Multiply a Scalar with a Vector and a Matrix | ||
| Invert a Matrix | ||
| Compare Vectors | ||
| (De)serialize a Matrix |
Statistics
| Recipe | Crates | Categories |
|---|---|---|
| Calculate Measures of Central Tendency | ||
| Compute the Standard Deviation |
Trigonometry
| Recipe | Crates | Categories |
|---|---|---|
| Calculate the Side Length of a Triangle | ||
Verify that tan is Equal to sin Divided by cos | ||
| Calculate the Distance Between two Points on Earth |
Code Organization
Code Organization by Project Type
Dependencies
Modules and Paths
| Structure your Code into Modules |
| Split your Code into Several Files |
| Access Items Within Modules via Paths |
| Use Modules to Hide Implementation Details |
Naming Conventions
| Name your Functions, Types, etc. (Naming Conventions) |
Decide Between as_* vs to_* vs into_* for Conversion Methods |
| Name Getters |
| Name Iterators |
Use Declarations
Visibility
Crate Selection
Current State of The Art
Indices
Introduction
How to Read This Book
| How to Use the Code Examples |
| Additional Examples |
| A Note about Crate Representation |
| A Note about Error Handling |
Language
Attributes
Closures
| Closure Syntax |
| Force Closure Capture by Value |
| Annotate Closure Types |
| Use Closures as Function Arguments |
Constants and Statics
Control Flow
Execute Code based on a Condition Using if and else |
Create Loops Using loop |
Execute Code Repeatedly While a Condition is True with while |
Iterate Through a Collection with for |
| Exit Early from Blocks |
Data Types
| Use Scalar Data Types |
| Declare and Use Compound Data Types: Tuples and Arrays |
| Use the Unit and Never Special Types |
| Declare Strings |
| Declare a Type Alias |
Entry Points
| Define the Entry Point of your Application with the Main Function |
| Define the Entry Point of your Asynchronous Application |
Enumerations
Functions
| Write a Rust Function |
| Write a Generic Function |
| Return a Reference from a Function |
| Work with Diverging Functions |
| Work with Function Pointers |
Generics
Impl Trait
Simplify Method Signatures with impl Trait |
Return Opaque Types (esp. Closures and Iterators) with Return-position impl Trait |
Iterators
Lifetimes
Macros
Ownership and Borrowing
Pattern Matching
Slices
| Slice Basics |
| Create Slices from Arrays or Vectors |
| String Slices |
| Use a Slice as a Function Argument |
| Common Use Cases |
Structs
Trait Objects and Dynamic Dispatch
Traits
Variables
Links
Blogs and Forums
| Chat Platforms, Forums, and Social Media |
| Rust Blogs and Newsletters |
| Personal Blogs |
| Project and Organization Blogs |
Books
Example Code
Learning Rust
| Rust Overviews and Starting Points |
| Learning Roadmaps |
| Tutorials and Workshop Materials |
| Guides |
| Courses |
| University Courses |
| Ferrous Systems Training |
| Paid Courses |
| Others |
| Lists of Rust Links and Resources |
Meetings
Rust Cheatsheets
Rust Companies and Jobs
Videos and Podcasts
Rust Installation
Standard Library
Automatic Trait Derivation
Borrowed Types
| Recipe | Crates |
|---|---|
Use the Borrow Trait to Work with Borrowed Types Synonymous with Owned Types | |
Differences between Borrow, Deref, and AsRef in Generic Code | |
Implement Borrow for a Custom Type |
Clone-On-Write
Conversion Traits
Default Values
| Recipe | Crates |
|---|---|
Define Default Values for a Struct with Default |
Dynamic Typing
| Recipe | Crates |
|---|---|
Determine the Type of an Object at Runtime and Downcast with the Any Trait | |
| Compile-time Reflection |
Heap Storage
Interior Mutability
| Recipe | Crates |
|---|---|
Use Cell | |
Use RefCell | |
Use OnceCell |
Memory Address Pinning
| Recipe | Crates |
|---|---|
Give Data a Stable Memory Address with Pin |
Operators
Optional Values
Recoverable Error Handling
| Recipe | Crates |
|---|---|
Handle Recoverable Errors with Result | |
Retrieve or Transform Values in Result | |
Get Reference to Values Inside a Result | |
Choose between Result and Option |
Reference-To-Reference Conversions
| Recipe | Crates |
|---|---|
Accept Arguments of Multiple Types with AsRef | |
Use as_ref to Get a Reference to the Contained Value of a Smart Pointer | |
Choose between AsRef, Deref, and Borrow |
Reference Counting
| Recipe | Crates |
|---|---|
Share Ownership of a Value with Rc | |
Share Ownership of a Value with Interior Mutability via Rc and RefCell | |
Share Ownership of a Value with Rc |
Resource Cleanup
Smart Pointers
| Recipe | Crates |
|---|---|
Implement a Smart Pointer with Deref | |
| Smart Pointer Comparison | |
| Multiple Ownership with Reference Counting | |
| Interior Mutability | |
| Lazy Initialization | |
| Synchronization Primitives |