rust
14 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use sqlx::PgPool;
#[derive(sqlx::FromRow)]
struct User {
id: i32,
name: String,
}
async fn get_user(pool: &PgPool, user_id: i32) -> Result<User, sqlx::Error> {
let user = sqlx::query_as!(User, "SELECT id, name FROM users WHERE id = $1", user_id)
.fetch_one(pool)
.await?;
Ok(user)
}
1 file · rust
Explain with highlit
Sqlx is a pure-Rust SQL client that checks queries at compile time against your database schema. The query! macro connects to your DB during compilation and validates that columns and types match. This catches typos and schema drift before runtime. It supports Postgres, MySQL, and SQLite, with async drivers. I use query_as! to map rows directly into structs. For dynamic queries, query() (no macro) falls back to runtime checks. Connection pooling is built-in via PgPool. The compile-time verification is the killer feature: refactoring is safe because the compiler tells you which queries break. For migrations, I use sqlx-cli. It's more ergonomic than ORMs while staying close to SQL.
Related snips
typescript
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
typescript
reliability
retry
by codesnips
2 tabs
rust
struct Config<'a> {
name: &'a str,
value: &'a str,
}
fn parse_config(line: &str) -> Config {
Lifetime annotations for flexible borrowing in structs
rust
lifetimes
borrowing
by Marcus Chen
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
javascript
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
const success = true;
setTimeout(() => {
if (success) {
Promises and async/await patterns for asynchronous JavaScript
javascript
promises
async-await
by Alex Chang
1 tab
rust
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
rust
observability
tracing
by Marcus Chen
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.