rust

Lifetime annotations for flexible borrowing in structs

When a struct holds a reference, you must annotate its lifetime so the compiler knows the reference won't outlive the data. The syntax <'a> declares a lifetime parameter, and &'a str ties the reference to that lifetime. This ensures that as

Crossbeam for advanced concurrent data structures

Crossbeam provides lock-free data structures and utilities for concurrent programming. The crossbeam-channel crate offers multi-producer multi-consumer channels with better performance than std::sync::mpsc. crossbeam-utils has scoped threads (borrow d

tracing for structured logging and distributed tracing

The tracing crate is the modern standard for instrumentation in async Rust. It provides structured logging with spans (representing work) and events (point-in-time records). Spans can be nested, creating a tree that represents causality. I instrument

Channels (mpsc) for message passing between threads

Rust's mpsc (multiple producer, single consumer) channels are the safest way to communicate between threads. You send owned values through the channel, transferring ownership to the receiver. This prevents data races because only one thread owns the d

clap for CLI argument parsing with derive macros

For CLI tools, clap is the de facto standard. Version 4+ supports derive macros, letting you define arguments as a struct with attributes. The library auto-generates help text, validates inputs, and supports subcommands. I annotate fields with #[arg(s

Declarative macros (macro_rules!) for code generation

Declarative macros (macro_rules!) let you write code that writes code, reducing boilerplate. They pattern-match on token trees and expand at compile time. I use them for repetitive patterns like implementing traits for multiple types or generating tes

Integration tests in tests/ directory

Integration tests go in the tests/ directory and are compiled as separate binaries. Each file in tests/ is a separate test crate with access only to your crate's public API. This enforces API-level testing and catches issues that unit tests miss. I us

Cow for clone-on-write to avoid unnecessary allocations

Cow<'a, T> (clone on write) holds either a borrowed or owned value. It borrows when possible and clones only when mutation is needed. I use Cow<str> for APIs that might need to modify a string: if no changes are needed, it stays borrowed;

anyhow::Context for adding error context without custom types

When prototyping or writing applications (not libraries), anyhow is my go-to. It provides Result<T> as an alias for Result<T, anyhow::Error>, which can hold any error type. The .context() method attaches additional context to errors as the

Builder pattern for complex struct initialization

For structs with many optional fields, the builder pattern provides a fluent API for construction. I define a separate Builder struct with methods that return self for chaining. The final build() method validates and returns the target struct. This is

Send and Sync traits for safe concurrency guarantees

Send means a type can be transferred across thread boundaries. Sync means a type can be shared between threads (&T is Send). Most types are Send + Sync; exceptions include Rc (not Send) and RefCell (not Sync). The compiler uses these marker traits

const and const fn for compile-time evaluation

const defines compile-time constants, and const fn are functions that can run at compile time. I use const for magic numbers, lookup tables, and configuration that never changes. const fn is powerful for building complex constants (like hash maps or a