admin
@admin ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes

Showing 481–510 of 770 public snippets

Bash
Find + Replace Across Many Files
Use `find` + `sed -i` to do project-wide refactors. Always test the regex first with a dry run.
6h ago 0
Rust
Parse Strings to Typed Values
`str::parse::<T>()` works for every type that implements `FromStr` — every numeric type, `bool`, `IpAddr`, `Uuid` (with the crate), etc. Returns `Result` so you can chain with `?`.
6h ago 0
Python
functools.cache Memoization
Wrap any pure function with @cache (Python 3.9+) and get an unbounded memoization layer for free. Use @lru_cache(maxsize=N) when you need a bounded cache.
6h ago 0
Bash
cURL GET with Timeout + Retry
A sensible default cURL invocation: short connect timeout, total timeout, retry on transient failures with backoff, fail-on-error.
6h ago 0
Bash
OS / Distro Detection
Read /etc/os-release for distro info — it's the standard on every modern Linux. Branch deploy scripts on distro to pick apt vs. dnf vs. apk.
6h ago 0
Rust
Slice Pattern: Borrow Without Copy
A slice (`&[T]`) is a borrowed view into a contiguous sequence. Pass `&v[..]` or just `&v` instead of `v.clone()` when the function only needs to read.
6h ago 0
Rust
Unit Tests with #[test] and assert_eq!
Tests live alongside the code they test. `cargo test` runs every `#[test]` function. Use `#[cfg(test)] mod tests` so the test code is compiled out of release builds.
6h ago 0
HTML
Progress and Meter Elements
`<progress>` shows how much of a task is done (downloads, multi-step forms). `<meter>` shows a value within a known range (disk usage, password strength). Both render as native UI in every browser.
6h ago 0
TypeScript
uniqueBy — Dedupe by Callback
Deduplicate an array using a derived key (object id, lowercased email, etc.). First occurrence wins. Backed by a Map for O(n) performance.
6h ago 0
Go
os.Getenv with Defaults
`os.Getenv` returns "" when unset — easy to miss. Wrap it with a typed helper that supplies defaults and parses numerics. Saves boilerplate at every config-read site.
6h ago 0
Python
ProcessPoolExecutor for CPU-Bound Work
Use processes (not threads) for CPU-bound work to sidestep the GIL. Functions must be picklable — define them at module level, not as locals or lambdas.
6h ago 0
Go
Struct Embedding (Composition over Inheritance)
Go has no inheritance. Embed a type to "promote" its fields and methods as if they were on the outer type. Same effect as inheritance for most purposes, but explicit.
6h ago 0
PHP
Safe Path Join
Concatenate path segments and produce a normalized canonical path that resists "../" escape attempts. Throws if the result would land outside the given base directory.
6h ago 0
Python
textwrap dedent + fill (Multi-line Strings)
`textwrap.dedent` strips common leading whitespace from a triple-quoted string — finally, you can indent multiline strings inside functions without breaking the formatting. `fill` wraps to a max width.
6h ago 0
PHP
TOTP Code Generate + Verify
Generate and verify a 6-digit time-based one-time password (RFC 6238) compatible with Google Authenticator / Authy. Uses a base32-encoded secret and 30-second time steps.
6h ago 0
PHP
Generate Secure API Key
Mint an API key with a recognizable prefix ("sk_live_…") and 32 bytes of crypto-random entropy encoded as URL-safe base64. Stripe-style readable IDs.
6h ago 0
Bash
Generate UUID
Several portable ways to mint a v4 UUID from the shell — useful for request IDs, idempotency keys, temporary filenames.
6h ago 0
PHP
RGB ↔ Hex Conversion
Two-way conversion between #RRGGBB hex strings and [R, G, B] arrays. Handy when working with theme colors that come from both sources (CSS strings vs. RGB sliders).
6h ago 0
Bash
Atomic File Write (write then rename)
Write to a temp file in the SAME directory, then `mv` to the final name. mv on the same filesystem is atomic — readers never see a half-written file.
6h ago 0
Bash
Read File Line-By-Line (the safe way)
The classic `for line in $(cat file)` is wrong — it word-splits and globbing fires on filenames. Use `while IFS= read -r line` with input redirection.
6h ago 0
Python
Safe JSON Read/Write
Idiomatic helpers for reading + writing JSON to disk, with atomic writes and pretty-printed output. Catches the common "edit and save → corrupt file on crash" failure mode.
6h ago 0
Rust
String vs &str — The Cheat Sheet
`String` is an owned, growable heap allocation; `&str` is a borrowed view into a UTF-8 buffer. Function params should usually take `&str`; return types use `String` when ownership is needed.
6h ago 0
Go
CSV Read/Write with encoding/csv
`encoding/csv` parses RFC 4180 CSVs. Read row-by-row with `Read()` or all at once with `ReadAll()`. Same for writing — flush with `Writer.Flush()` before closing.
6h ago 0
Rust
Atomic File Write (tempfile + persist)
Write to a temp file in the SAME directory, then atomically `persist` (rename). The `tempfile` crate handles cleanup if you crash before persisting.
6h ago 0
Go
JSON API Endpoint
Decode an incoming JSON body into a typed struct, do work, encode a JSON response. The whole roundtrip is ~20 lines of standard library.
6h ago 0
PHP
Atomic File Write
Write a file in a way that other processes never see a half-written state. Write to a temp file in the same directory, then rename — rename is atomic on POSIX filesystems.
6h ago 0
Bash
Associative Array (hash map)
Bash 4+ supports key-value associative arrays. Must `declare -A` first — Bash won't infer it. Iterate keys with !arr[@].
6h ago 0
Bash
SSH to Multiple Hosts in Parallel
Run the same command on a fleet of hosts. Three patterns: serial loop (simplest), GNU parallel (fastest), pssh (purpose-built).
6h ago 0
Rust
axum with Shared State + Extractors
Use `State` to inject shared application state (DB pool, config, etc.) into every handler. `Path` and `Json` extractors deserialize URL params and request bodies for you.
6h ago 0
JavaScript
Memoize Function
Caches the result of a pure function keyed by its serialised arguments. On repeated calls with the same inputs the cached value is returned instantly, skipping expensive computation. Supports single and multi-argument functions via JSON key serialisation.
6h ago 0