tezvyn:

⚙️Backend Dev

Backend engineering, APIs, and databases

1085 bites

More in Backend Dev — page 42

Go & Rust2 min read

Rust's Deref Trait: Smart Pointers Acting Like Data

The `Deref` trait lets a "smart pointer" type act like the data it contains, making wrappers transparent. It enables calling an inner type's methods directly on a wrapper, like using `&str` methods on a `String`. Its `deref()` method must never fail.

Go & Rust2 min read

Go's Functional Options Pattern for Flexible APIs

The functional options pattern uses functions to set optional struct fields, making APIs flexible and readable. It's common for complex constructors like servers or DB clients.

Go's Worker Pool Pattern: Capping Concurrency
Go & Rust2 min read

Go's Worker Pool Pattern: Capping Concurrency

A worker pool caps concurrency by using a fixed number of goroutines to process jobs from a queue. Use it for rate-limiting API calls or processing files without spawning unlimited goroutines.

Go & Rust2 min read

The Builder Pattern: Constructing Complex Objects in Rust

The Builder pattern lets you construct complex objects step-by-step using a chain of method calls. It's crucial in Rust for structs with many optional fields, since the language lacks default arguments.

Go & Rust2 min read

Rust's `clap`: Build CLIs by Describing Them

`clap` lets you define a Rust struct representing your CLI's arguments, and it generates the parser, help text, and validation. It's used for building any Rust CLI, but its feature-richness can increase binary size over simpler alternatives.

Go Cobra: Build Complex CLIs Like `kubectl`
Go & Rust2 min read

Go Cobra: Build Complex CLIs Like `kubectl`

Cobra gives your Go CLI a command tree, like `git remote add`. It's for apps with nested commands and persistent flags, not just simple tools. The footgun is using it for a single command when Go's `flag` package would suffice.

Go & Rust2 min read

cbindgen: Auto-generate C/C++ Headers for Rust

cbindgen automatically generates C/C++ headers for your Rust code, saving you from writing tedious FFI boilerplate. Use it when exposing a Rust library to other languages. Its feature set is ad-hoc, so it may not support your specific edge case out of the box.

Go & Rust2 min read

Rust's `bindgen`: Auto-Generate FFI to C/C++

`bindgen` is a translator that reads C/C++ headers and writes the unsafe Rust FFI code to call them. It's used to integrate Rust with existing C libraries, like system APIs or legacy code, saving you from writing bindings by hand.

Go & Rust2 min read

Rust: Expose Functions to C with `#[no_mangle]`

The `#[no_mangle]` attribute tells the Rust compiler not to alter a function's name, exposing a stable symbol for C code to call. Use it with `extern "C"` to create Rust libraries for other languages. The footgun is forgetting `extern "C"`, causing crashes.

Go & Rust2 min read

Rust: Bridging C Strings with CStr and CString

CString and CStr are Rust's safe wrappers for C's nul-terminated strings. CString builds a C-compatible string to pass *out* of Rust; CStr interprets one coming *in*. Use them for any FFI calls.

Go & Rust2 min read

Rust's `libc` Crate: Speaking the OS's Language

The `libc` crate is Rust's dictionary for C types, letting you talk to the OS. Use it for system calls or linking C libraries, like when building low-level network tools.

Go & Rust2 min read

Rust's `extern` Block: Talking to Other Languages

An `extern` block is Rust's contract for calling code from other languages, like C. You declare external functions and statics, promising they exist. Use it for FFI to call system libraries, but know all calls are `unsafe` as Rust can't verify them.

Go & Rust2 min read

Go Assembly: A Semi-Abstract Instruction Set

Go's assembler isn't a direct mapping to machine code; it's a semi-abstract instruction set. A `MOV` might become a `clear` or `load`. This is what you see with `go tool compile -S`. The footgun is assuming your assembly maps 1:1 to the final machine code.

Go & Rust2 min read

Rust's Pin: Fixing a Value's Memory Address

Pin<P> tells the Rust compiler a value must not move from its memory location. Think of it as nailing an object to a specific spot on the memory shelf. This is crucial for self-referential types, like those in async runtimes.

Go & Rust2 min read

Rust Const Generics: Parameterize by Value, Not Just Type

Const generics let Rust types be parameterized by values, not just other types. This allows writing code generic over array sizes, like `Matrix<T, const N: usize>`, ensuring dimensions are checked at compile time.

Go & Rust2 min read

Rust Procedural Macros: Code That Writes Code

Procedural macros are compile-time functions that write Rust code for you. They power common patterns like Serde's `#[derive(Serialize)]`. The main footgun is hygiene: generated code can clash with local variables, so authors must use absolute paths to be…

Go & Rust2 min read

Cgo: The Bridge Between Go and C Code

Cgo is Go's bridge to the C world, letting you call C functions and use C types from your Go code. It's for leveraging existing C libraries or low-level OS APIs. The footgun: cgo calls have high overhead and break Go's simple cross-compilation.

Go & Rust2 min read

Go's `unsafe` Package: Breaking the Rules for Performance

Go's `unsafe` package lets you bypass type safety, treating memory like C with raw pointers for performance gains. It's used for low-level optimizations and C interoperability. The footgun: its behavior isn't guaranteed across Go versions, making code fragile.

Go & Rust2 min read

Rust Raw Pointers: When References Aren't Enough

Raw pointers (*const T, *mut T) are Rust's C-style pointers, bypassing the borrow checker. They're used for FFI or building low-level abstractions. The footgun is assuming they're safe; they can be null or dangling, requiring `unsafe` to dereference.

Go & Rust2 min read

Rust's `unsafe` Keyword: Five Superpowers, Zero Guarantees

Rust's `unsafe` keyword lets you bypass certain compile-time memory safety guarantees for low-level tasks like OS interaction or FFI. The footgun is thinking it disables all safety; it only enables five specific 'superpowers,' making you responsible for…