rust

Arc and Mutex for safe shared mutable state across threads

When you need shared mutable state across threads, Arc<Mutex<T>> is the idiomatic pattern. Arc is an atomic reference counter that allows multiple ownership, and Mutex provides interior mutability with runtime locking. You clone the Arc fo

Default trait for sensible zero values

The Default trait provides a default value for a type, useful for builder patterns, config merging, and initialization. I derive Default on structs where zero/empty is meaningful. For custom logic, implement it manually. Default::default() is the stan

Feature flags for conditional compilation

Cargo's feature flags let you enable or disable parts of your crate at compile time. I use them for optional dependencies (like serde), platform-specific code, or different build profiles. Features are defined in Cargo.toml and checked with #[cfg(feat

serde for zero-copy serialization and deserialization

Serde is Rust's serialization framework, supporting JSON, YAML, TOML, MessagePack, and more through format-specific crates. With #[derive(Serialize, Deserialize)], your structs automatically convert to and from these formats. Serde is extremely fast b

panic! and unwinding for unrecoverable errors

Rust panics are for bugs, not expected errors. When code panics (via panic!, unwrap(), expect(), or assertion failure), the thread unwinds by default, running destructors. I use panic! for invariant violations or \"this should never happen\" cases. Fo

nom for parser combinators and zero-copy parsing

Nom is a parser combinator library for building parsers from small, composable functions. It's byte-oriented and zero-copy, making it ideal for binary protocols, config files, or log parsing. I define parsers for tokens (like tag("GET") or digit1), th

sqlx for compile-time checked SQL queries with async

Sqlx is a pure-Rust SQL client that checks queries at compile time against your database schema. The query! macro connects to your DB during compilation and validates that columns and types match. This catches typos and schema drift before runtime. It

Newtype pattern for type-safe primitives

The newtype pattern wraps a primitive in a tuple struct to create a distinct type. This prevents mixing up values that are semantically different but have the same underlying type (like UserId(u32) vs PostId(u32)). The compiler enforces that you can't

tokio::spawn for concurrent task execution

Tokio's spawn creates a new async task that runs concurrently on the runtime's thread pool. Unlike OS threads, tasks are lightweight (kilobytes of memory) and scheduled cooperatively. Each task must be 'static and Send, meaning it can't borrow local v

cfg attribute for conditional compilation

The #[cfg(...)] attribute enables conditional compilation based on features, target OS, or build profiles. I use #[cfg(test)] for test-only code, #[cfg(target_os = \"linux\")] for platform-specific logic, and #[cfg(feature = \"...\")] for optional fea

Derive macros for automatic trait implementations

Rust's #[derive] attribute auto-generates trait implementations for common traits like Debug, Clone, PartialEq, and Serialize. This eliminates boilerplate and ensures consistency. For example, #[derive(Debug)] generates a debug formatter that prints a

Cargo workspaces for multi-crate projects

For larger projects, I organize code into multiple crates within a Cargo workspace. The root Cargo.toml lists workspace members, and each crate has its own dependencies and Cargo.toml. This lets you split code into libraries and binaries, share code b