More in Backend Dev — page 44

Buffered I/O: Batch System Calls for Speed
Buffered I/O batches many small reads or writes into fewer, larger system calls, trading a small amount of memory for a huge speed boost. It's essential for tasks like writing log files line-by-line, preventing a system call for every single line.

Go vs. Rust: Why String Indexing Is Tricky
Rust prevents direct string indexing to force correctness, while Go treats strings as raw byte slices. This matters for non-ASCII text where characters span multiple bytes. The footgun: Go's `s[i]` can corrupt data; Rust's `&s[..i]` can panic.
Go's Time: Wall Clocks vs. Monotonic Clocks
Go separates telling time (wall clock) from measuring it (monotonic clock) to prevent errors from system clock changes. Use `time.Sub` for reliable timing and `time.Format` for display. The footgun: formatting uses a magic date, not YYYY-MM-DD.
Go-Style vs. GNU-Style Flag Parsing
Go's command-line parser is stricter than the familiar GNU style, not distinguishing short/long flags or allowing them after arguments. This is key when porting Go CLIs to Rust to maintain user experience.
Go's `os` Package: Your File System Toolkit
Go's `os` package is your universal remote for the file system. Use `ReadFile` for quick access or open a `File` object for finer control. It's essential for logs and configs. Forgetting to `Close()` a file leaks resources and can crash your program.
Rust Build Profiles: Tune for Speed vs. Debugging
Rust build profiles are presets for the compiler, trading off compile time and debuggability for runtime speed. Use the `dev` profile for quick iteration and the `release` profile for production. The footgun is benchmarking without the `--release` flag.
Rust Cargo Features: Conditional Compilation & Dependencies
Cargo features are compile-time switches for conditional compilation and optional dependencies. They let you build tailored versions of a crate from one source, like an image library that only includes code for the formats you need.
Rust Build Scripts: Compiling More Than Just Rust
A `build.rs` script is a pre-compilation hook for tasks outside Rust's scope, like compiling C code or generating Rust modules. It's essential for FFI or code generation. A key footgun: `cfg!` checks the host, not the target, breaking cross-compilation.
cargo doc: Turn Code Comments into a Website
cargo doc turns your Rust doc comments into a searchable HTML website for your crate and its dependencies. Use it to generate a local API reference or explore a dependency's API.
Go's Race Detector: Find Concurrency Bugs at Runtime
The Go race detector finds data races by watching memory access at runtime. Use `go test -race` in CI or on a canary instance, but remember: it only catches races that actually execute. If your tests don't trigger the race, it won't be found.
Go Benchmarking: Measure, Don't Guess
Go's benchmark runner finds stable performance numbers by repeatedly calling your code in a loop controlled by b.N. Use it to optimize hot paths or compare algorithm implementations. Forgetting b.ResetTimer() will include setup costs, skewing your results.
Cargo Clippy: Your Opinionated Rust Code Reviewer
cargo clippy is an automated code reviewer that goes beyond the compiler, catching subtle bugs, performance issues, and style violations. Run it in CI to enforce idiomatic Rust.
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.
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 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.
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.
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'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.
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.
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.