All bites
The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.
4247 bites
Page 131
Composable Error Types with `thiserror` in Rust
thiserror generates boilerplate for custom Rust error types, letting you define specific, matchable errors for a library. Use it when callers need to handle different failure modes. The footgun is using it for simple app errors where anyhow would suffice.
Implicit Interface Satisfaction in Go
Go types satisfy an interface automatically by having the right methods, with no explicit implements declaration. This structural typing decouples implementations from interface definitions, so you can define interfaces around how you use a type without…
Static Dispatch: Zero-Cost Abstraction via Monomorphization
Static dispatch resolves function calls at compile time, avoiding runtime overhead. Rust does this via monomorphization, creating specialized code for each concrete type. This is the default for generics, but the trade-off is larger binary sizes.
Rust's `impl Trait`: Hiding Concrete Types
Rust's impl Trait specifies a type by its behavior, not its name. Use it in function arguments for cleaner generics (fn f(x: impl Debug)) or in return types to hide complex types like closures and iterators, avoiding heap allocation.
Rust Associated Types: One Trait, One Concrete Type
Associated types link a placeholder type to a trait, ensuring any implementation provides one specific type. This cleans up code, like in Rust's Iterator trait. The footgun: a type can only implement a trait with an associated type once.
Rust Marker Traits: Properties as Types
Marker traits are empty labels telling the Rust compiler about a type's capabilities, like being copyable or thread-safe. They have no methods; their presence is the signal. They're key for concurrency (Send/Sync) and memory (Copy/Sized) safety checks.
Rust's std::thread::spawn: Create and Manage OS Threads
std::thread::spawn creates a new OS thread to run code concurrently, returning a JoinHandle to wait for completion. Use it for background tasks or parallel computations. The footgun: dropping the handle detaches the thread, risking resource leaks.
Rust Channels: Thread-Safe Communication
Rust channels are like a thread-safe conveyor belt for sending data between threads. Use them to pass work to workers or aggregate results. The footgun: the receiver blocks forever if any sender isn't dropped, as the channel only closes when all senders are…
Go Channels: Buffered vs. Unbuffered
Unbuffered channels are a synchronous rendezvous, blocking until both sender and receiver are ready. Buffered channels are an async mailbox, letting senders drop messages and go. The footgun is using a buffer to hide a deadlock instead of fixing it.
Rust's async/await: Cooperative Concurrency
Rust's async/await is cooperative concurrency, where tasks explicitly yield control with .await. This is ideal for I/O-bound work like managing thousands of network connections. The biggest footgun: calling an async function without .await does nothing.
Rust Async Runtimes: The Engine for `async/await`
Rust's async/await is just syntax; an async runtime like Tokio is the engine that runs the code. It polls Futures until they complete, managing I/O and scheduling. This is essential for web servers.
Rust's `std::sync::Mutex`: Guarding Shared Data
A Rust Mutex guards shared data, granting access only via a temporary RAII "guard" that auto-releases the lock. It's used inside an Arc for safe multi-threaded mutation.
Send vs. Sync: Rust's Thread Safety Contracts
Send means a value can move to another thread; Sync means references to it can be shared. They are the compiler's contracts for preventing data races. The compiler checks them when you spawn threads.
CSP: Model Concurrency with Message Passing
CSP treats concurrency as isolated processes talking through channels, not threads fighting over shared memory. It shaped Go, Erlang, and occam. Engineers often retrofit shared-state patterns into channel-based code and reintroduce race conditions.
Go's Memory Model: Don't Be Clever
Go guarantees your program behaves predictably—as if on one CPU—if you prevent data races. Use channels or sync primitives to serialize access when goroutines share data. The footgun is relying on timing instead of explicit synchronization.
Rust's Scoped Threads: Borrowing Across Threads Safely
Scoped threads let you borrow local variables from a parent thread without complex wrappers. The scope guarantees all spawned threads are joined before it exits, satisfying the borrow checker. Use it to parallelize work on stack data.
Go Build: From Source Code to Executable
go build is your factory for turning Go source into a runnable program. It compiles your packages and their dependencies into a single executable. Use it to create a binary for deployment, but don't confuse it with go install which puts the file in your.
cargo build: Compile Your Rust Package and Its Dependencies
Think of cargo build as your project's general contractor. It reads the Cargo.toml blueprint to compile your package and all its dependencies. A common footgun is forgetting it only builds libraries and binaries by default; use --tests for test targets.
cargo test: Rust's All-in-One Test Runner
cargo test is Rust's built-in test runner, automatically discovering and executing unit, integration, and documentation tests. Use it to validate code marked with #[test] and examples in docs.
cargo add: Stop Editing Cargo.toml By Hand
Stop editing Cargo.toml by hand. cargo add lets you add, remove, and modify Rust dependencies from the command line. Use it to pull crates from registries, git repos, or local paths.