Functional Iterator Adapters
Iterators in Rust are lazy and powerful. 'filter' and 'map' allow for functional-style data transformation without unnecessary intermediate allocations.
Open snippet →Read these Intermediate Rust snippets line by line — each one comes with a written breakdown of what the code does and why.
Iterators in Rust are lazy and powerful. 'filter' and 'map' allow for functional-style data transformation without unnecessary intermediate allocations.
Open snippet →Generic bounds allow you to restrict a generic type so that it must implement specific behaviors (traits). This ensures the function can call the required methods safely.
Open snippet →Rust enums are algebraic data types. They can store different types of data in each variant, which can be elegantly extracted using 'match'.
Open snippet →The '?' operator is a concise way to handle errors. If a Result is an Err, it returns early from the function with that error; otherwise, it unwraps the Ok value.
Open snippet →Lifetimes ensure that references remain valid as long as they are needed. Annotating a struct with '<'a>' tells the compiler the struct cannot outlive the reference it holds.
Open snippet →Arc (Atomic Reference Counted) allows multiple threads to own the same data, while Mutex (Mutual Exclusion) ensures that only one thread can mutate the data at a time.
Open snippet →Raw pointers (*const T and *mut T) allow you to bypass Rust's ownership and borrowing rules. Dereferencing them is considered unsafe because the compiler cannot guarantee the pointer is valid or th…
Open snippet →The Entry API allows you to check for a key's existence and perform an operation in a single step, avoiding double lookups and providing a more expressive way to handle conditional insertion.
Open snippet →RefCell<T> provides 'interior mutability', a pattern allowing you to mutate data even when you have an immutable reference to that data. Borrowing rules are enforced at runtime rather than compile…
Open snippet →The 'move' keyword forces a closure to take ownership of the variables it captures from the environment, rather than just borrowing them. This is often required when passing closures to new threads.
Open snippet →Rust has no null values. Instead, Option<T> represents optional values: Some(T) contains a value, None represents absence. This makes null pointer exceptions impossible at compile time. The if let…
Open snippet →Rust iterators are lazy — they do nothing until consumed. Methods like filter, map, and take are adaptors that transform iterators without executing. The collect method consumes the iterator and pr…
Open snippet →impl blocks define methods on structs. &self borrows the struct immutably, &mut self borrows mutably, and no self creates an associated function (constructor). Self refers to the implementing type.…
Open snippet →Result<T, E> represents recoverable errors: Ok(T) for success, Err(E) for failure. The ? operator propagates errors early, returning from the function immediately on Err. This is idiomatic Rust err…
Open snippet →Lifetimes are Rust's way of ensuring memory safety without garbage collection. They track how long references are valid. The 'a lifetime annotation connects the input and output references, guarant…
Open snippet →Trait bounds constrain generic types to only accept values that implement certain behaviors. You can combine multiple bounds with +. The impl Trait syntax provides a shorter way to specify constrai…
Open snippet →Closures can capture variables from their surrounding environment. Rust determines the capture mode automatically based on how the closure uses the variable: by reference, by mutable reference, or…
Open snippet →Slices let you reference a contiguous sequence of elements within a collection without taking ownership. The slice type is written as &[T] for an array or &str for string slices. String slices are…
Open snippet →Cell types enable mutable data sharing in single-threaded scenarios where borrowing rules would normally prevent it. Cell<T> works with Copy types only, providing set and get methods. RefCell<T> wr…
Open snippet →Associated types allow you to define placeholder types within a trait that implementing types must specify. Unlike generic type parameters, associated types enforce a single concrete type per imple…
Open snippet →Custom error types encapsulate domain-specific failure modes. By implementing Display, errors become human-readable. The Error trait enables composability with Rust's ? operator and error propagati…
Open snippet →Rust provides graduated error handling strategies. catch_unwind captures panics for graceful recovery. expect provides clear error messages for Option/Result. unwrap_or offers fallback values. get…
Open snippet →Const generics allow you to use constants as type parameters, enabling compile-time parameterized types. Unlike C++ templates with non-type parameters, Rust validates all const generic bounds at co…
Open snippet →The newtype pattern wraps primitive types in tuple structs to create distinct types at zero runtime cost. This provides compile-time unit checking—Rust will refuse to pass Kilograms where Meters ar…
Open snippet →OnceLock provides thread-safe lazy initialization for static data. The configuration is only read from disk when first accessed, not at program startup. This improves startup time if the config is…
Open snippet →Unsafe Rust grants direct memory access beyond what safe Rust permits. Raw pointers (*const T, *mut T) can be dereferenced only in unsafe blocks, acknowledging undefined behavior risks. MaybeUninit…
Open snippet →Match expressions in Rust provide powerful pattern matching capabilities. You can match on enum variants, include guard clauses with 'if' conditions, and destructure complex data directly within th…
Open snippet →Rust iterators use lazy evaluation, meaning operations are not executed until consumed by a terminal operation like 'collect', 'sum', or 'for_each'. This allows chaining multiple adaptors like filt…
Open snippet →The 'impl' block defines methods and associated functions for structs. Associated functions like 'from_celsius' and 'from_fahrenheit' don't take 'self' and serve as constructors, using 'Self' as a…
Open snippet →Box<T> allocates values on the heap instead of the stack. It's the primary way to create singly-linked data structures like trees and linked lists in Rust. Box<T> implements the Deref trait, so you…
Open snippet →