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.
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 →Pattern matching with match expressions is Rust's powerful way to handle enums. The compiler ensures all cases are covered, making code exhaustive and safe. Unlike switch statements in other langua…
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's async/await syntax builds on the Future trait. A Future represents a value that may not be available yet. The poll method is called by the executor to check if the async operation has comple…
Open snippet →Rc<T> provides shared ownership via reference counting. Each clone increments the count, each drop decrements it. When count reaches zero, the value is deallocated. However,Rc cannot detect referen…
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 →Const functions execute at compile time when called in const contexts, enabling true zero-cost abstractions. Unlike C++ constexpr, Rust const fn has stricter rules but is fully evaluated at compile…
Open snippet →