0 day streak
Code library

Expert Rust exercises

Read these Expert Rust snippets line by line — each one comes with a written breakdown of what the code does and why.

Rust · Expert

Pinned Memory for Async State Machines

The 'Pin' wrapper ensures that a value stays at a fixed memory address. This is critical for self-referential structs where a field stores a pointer to another field within the same object. If the…

Memory ManagementAsync & Concurrency
Open snippet →
Rust · Expert

Optimizing with UnsafeCell

'UnsafeCell' is the only legal way to obtain a mutable reference from an immutable one in Rust. It is the primitive behind 'Cell' and 'RefCell'. Using it directly is highly unsafe and requires the…

Memory ManagementPerformance
Open snippet →
Rust · Expert

Manual Send/Sync for FFI Wrappers

By default, types containing raw pointers are neither Send nor Sync. To pass them between threads or share references across threads, you must manually implement these marker traits. This requires…

Memory ManagementPerformance
Open snippet →
Rust · Expert

Higher-Rank Trait Bounds (HRTB)

Higher-Rank Trait Bounds (HRTB) allow you to specify that a bound holds for all possible lifetimes. The 'for<'a>' syntax ensures the closure can handle a reference with any lifetime, rather than a…

SyntaxFunctions
Open snippet →
Rust · Expert

Data-Level Parallelism with Portable SIMD

Portable SIMD (Single Instruction, Multiple Data) allows you to perform operations on multiple values simultaneously using CPU vector instructions. By packing four f32 values into a single f32x4 re…

PerformanceArrays & Lists
Open snippet →
Rust · Expert

Object Safety and Vtable Constraints

Not all traits can be used as dynamic trait objects (dyn Trait). For a trait to be 'object-safe', it cannot have generic methods or return 'Self' by value. However, you can keep a trait object-safe…

FunctionsMemory Management
Open snippet →
Rust · Expert

Designing Custom Dynamically Sized Types

Dynamically Sized Types (DSTs) like custom slices or header-prefixed arrays require manual memory allocation and fat pointer construction in Rust. We construct the layout dynamically, allocate raw…

Data TypesMemory Management
Open snippet →
Rust · Expert

Enforcing Lifetime Invariance via PhantomData

By default, references and types parameterizing lifetimes are covariant. However, types that allow mutation (like mutable references or cells) must be invariant over their type parameters and lifet…

Memory ManagementSyntaxBest Practices
Open snippet →