SaveSnippets
Community
Pricing
Sign In
Get Started
@admin
ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes
Showing
721–750
of
770
public snippets
Rust
Move vs Borrow — the Core Rule
Rust's ownership model: passing a value transfers ownership (move); taking a `&value` lets you read without taking ownership; `&mut value` lets you modify without taking ownership. Only one mutable borrow OR many immutable borrows at a time.
2h ago
0
TypeScript
useMediaQuery — Responsive Hook
Subscribe to a CSS media query (e.g. dark mode, viewport size) from React. Re-renders when the match changes. SSR-safe — returns `false` on the server.
2h ago
0
Python
TypedDict for JSON API Shapes
When you parse JSON, you want types — but creating a class for every payload is overkill. TypedDict gives you the static-checking benefits without the runtime overhead, and `total=False` marks every field optional.
2h ago
0
Python
NewType for Nominal IDs
Python's type system is structural — `UserId` and `PostId` are both just `int` unless you ask otherwise. `NewType` creates a distinct type with zero runtime cost for static checking only.
2h ago
0
HTML
Responsive Image — srcset + sizes
Let the browser pick the right image for the viewport. `srcset` lists candidates by intrinsic width; `sizes` tells the browser how big the image will render. Cuts mobile data dramatically.
2h ago
0
Java
try-with-resources
For any `AutoCloseable` (files, streams, DB connections, locks), declare it in `try(...)` and Java auto-calls `close()` even on exception. Replaces error-prone `try / finally` blocks.
2h ago
0
HTML
Responsive Table — Scroll on Mobile
Wrap any wide table in a horizontally-scrolling container. The table itself stays semantic (no display:block hacks that break screen readers); only the wrapper scrolls.
2h ago
0
Rust
Box<dyn Error> — Quick Boxed Errors
If you don't want the thiserror/anyhow dependency, `Box<dyn std::error::Error>` works as a catch-all. The `?` operator promotes any concrete error via `From`.
2h ago
0
Java
Multi-Catch + Exception Chaining
Catch several unrelated exception types in one block with `|`. Re-throw with `cause` to preserve the stack trace and underlying error.
2h ago
0
Java
Virtual Threads (Java 21+)
Java 21's virtual threads are millions-of-them cheap — JVM multiplexes them onto a small carrier pool. Replaces async/reactive code for most I/O-bound workloads: just write blocking code that doesn't actually block a kernel thread.
2h ago
0
Rust
Custom Error Type with thiserror
The `thiserror` crate generates a clean `Error + Display + Debug` impl from an enum, with automatic `From` conversions. The library-author's error-type tool of choice.
2h ago
0
Kotlin
Custom Exception + Sealed Result Pattern
For library / service code, define your own exception hierarchy. Pair with `sealed` + `when` for type-safe error handling at the call site.
2h ago
0
HTML
Multi-Column Footer
Standard site footer with grouped links, brand block, social icons, and legal/copyright. Use semantic `<nav aria-label>` on each column so screen readers announce the section labels.
2h ago
0
Java
Sealed Classes + Pattern Matching (Java 21+)
`sealed` restricts which classes can extend a type — perfect for closed hierarchies that pattern matching can switch over exhaustively. The compiler enforces that you handle every variant.
2h ago
0
PHP
Sort by Multiple Keys
Stable multi-column sort for arrays of associative rows. Each column can be sorted ASC or DESC independently. Wraps usort with a chained comparator.
2h ago
0
Rust
Lifetime Annotations Basics
Lifetimes are how Rust proves references don't outlive what they point to. Most are inferred — but functions returning references from arguments need explicit `'a` to relate input and output.
3h ago
0
PHP
Index Array By Column
Rekey a list of rows by one of their column values, so $byId[42] gives the row with id=42. Equivalent to array_column($rows, null, $key).
3h ago
0
Bash
Source Multiple Files from Directory
A common ~/.bashrc / ~/.zshrc pattern: split your config into ~/.bashrc.d/*.sh and source them all. Easier than one giant file.
3h ago
0
Bash
Sort an Array
Bash itself doesn't sort arrays — you pipe through `sort`. readarray captures the sorted output back into an array, preserving each element verbatim (including spaces).
3h ago
0
SQL
Gap Detection — Missing Days / Sequences
Find holes in a series — missing invoice numbers, days with no events, gaps in a sequence. Combine `LAG` (or `generate_series`) with a join to spot them.
3h ago
0
Python
Stdlib .env Loader (no dependency)
Parse a `.env` file into a dict and optionally export to os.environ. Skips comments and blank lines, strips surrounding quotes — no python-dotenv dependency needed.
3h ago
0
SQL
Pivot Without PIVOT (Conditional Aggregation)
Most databases don't have a real `PIVOT` keyword (SQL Server does). The portable answer is conditional aggregation — `SUM(CASE WHEN ...) AS col` for each pivoted value.
3h ago
0
Python
argparse with Subcommands
Standard-library CLI framework. Subcommands (like `git COMMAND`) require a sub-parser per command, each with its own arguments and handler.
3h ago
0
SQL
STRING_AGG / GROUP_CONCAT
Concatenate values across a group into a single delimited string. PostgreSQL/MSSQL use `STRING_AGG`; MySQL uses `GROUP_CONCAT`; SQLite has both.
3h ago
0
Go
maps package — Modern Helpers
`maps` (Go 1.21+) adds Keys, Values, Equal, Clone, Copy — replaces the slice-of-keys boilerplate you used to write to iterate a map deterministically.
3h ago
0
SQL
ROLLUP — Subtotals and Grand Totals
`GROUP BY ROLLUP` adds subtotal rows (with NULL for the rolled-up columns) plus a grand total. Drop into reports without writing UNIONs by hand.
3h ago
0
SQL
SELECT DISTINCT and Counting Uniques
`DISTINCT` removes duplicate rows. Combine with `COUNT(DISTINCT col)` to count uniques in aggregations — different from `COUNT(*)`.
3h ago
0
SQL
Sessionization — Group Events into Sessions
Stitch a stream of events into "sessions" where events more than N minutes apart start a new session. Uses LAG + a SUM-OVER trick to assign session IDs.
3h ago
0
SQL
Cohort Retention Analysis
Group users by their signup week, then count how many were still active in each subsequent week. The classic SaaS retention table — entirely in SQL.
3h ago
0
Bash
Idempotent Append to File
Add a line to a file (e.g., a config or PATH export) only if it isn't already present. Common in install/setup scripts that need to be safe to re-run.
3h ago
0
1
…
23
24
25
26