rust
14 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use std::pin::Pin;
use std::future::Future;
async fn example() {
println!("Example future");
}
fn spawn_local<F>(future: F)
where
F: Future<Output = ()> + 'static,
{
let pinned = Box::pin(future);
// Runtime would poll pinned future
}
1 file · rust
Explain with highlit
Pin ensures that a value won't move in memory, which is required for self-referential types like async futures. Most async code hides this complexity, but understanding it helps when you hit compiler errors about Unpin. A type is Unpin if it's safe to move even when pinned; most types are Unpin by default. When you write async fn, the compiler generates a state machine that might borrow across .await points, making it !Unpin. You pin futures with Box::pin() or pin!(). I rarely use Pin directly in application code, but it's essential for writing async combinators, custom runtimes, or intrusive data structures. The guarantees enable memory-safe async without garbage collection.
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.