Code Explainers

Rust code explainers

rust
use serde::Deserialize;
 
#[derive(Debug, Deserialize)]
#[serde(untagged)]

Parsing flexible JSON shapes with serde

deserialization enums json
Intermediate 6 steps
rust
use std::cmp::{Ordering, Reverse};
use std::collections::BinaryHeap;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};

K-way merge of sorted logs in Rust

binary-heap k-way-merge streaming-io
Intermediate 8 steps
rust
use axum::{
    extract::{Path, State},
    response::sse::{Event, KeepAlive, Sse},
};

Streaming import progress with SSE in Axum

server-sent-events streams watch-channel
Advanced 7 steps
rust
use std::f64::consts::PI;
 
#[derive(Debug, Clone, Copy)]
pub struct LatLng {

Building geographic bounding boxes in Rust

geospatial value-types option
Intermediate 7 steps
rust
use chrono::{Duration, NaiveDate};
 
#[derive(Debug)]
pub struct DateRange {

Parsing and iterating date ranges in Rust

error-handling iterators parsing
Intermediate 7 steps
rust
use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex};
use std::time::{Duration, Instant};
 

Building a counting semaphore in Rust

concurrency synchronization condition-variable
Advanced 9 steps
rust
use axum::{
    async_trait,
    extract::{rejection::JsonRejection, FromRequest, Request},
    http::StatusCode,

A validated JSON extractor in Axum

extractors validation error-handling
Intermediate 8 steps
rust
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::{interval, MissedTickBehavior};
 

A token-bucket rate limiter in Tokio

rate-limiting channels async
Advanced 8 steps
rust
use axum::{
    extract::FromRequestParts,
    http::{request::Parts, HeaderMap, StatusCode},
};

Building a replay-guard extractor in Axum

custom-extractor replay-protection shared-state
Advanced 8 steps
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

Opaque pagination cursors in Rust

pagination serialization base64
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::Request,
    http::{header::SET_COOKIE, HeaderValue},

Flash messages via Axum middleware

middleware shared-state cookies
Intermediate 8 steps
rust
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::thread;

Polling a directory for new files in Rust

polling filesystem callbacks
Intermediate 6 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts, State},
    http::{header::ACCEPT_LANGUAGE, request::Parts, StatusCode},
    response::{IntoResponse, Response},

A custom Accept-Language extractor in Axum

extractors internationalization http-headers
Advanced 8 steps
rust
use axum::{
    body::Body,
    extract::Request,
    http::{header, StatusCode},

A CSRF verification middleware in Axum

csrf middleware security
Intermediate 10 steps
rust
pub fn extract_quoted_fields(line: &str) -> Vec<String> {
    let mut fields = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;

Parsing quoted fields with a state machine in Rust

state-machine parsing string-processing
Intermediate 7 steps
rust
use std::sync::Mutex;
use std::time::{Duration, Instant};
 
#[derive(Debug, Clone, Copy, PartialEq)]

Building a circuit breaker in Rust

circuit-breaker state-machine concurrency
Advanced 8 steps
rust
use axum::{
    async_trait,
    extract::FromRequestParts,
    http::{header::HeaderValue, request::Parts, HeaderName},

Propagating trace context in Axum extractors

extractors distributed-tracing http-headers
Intermediate 8 steps
rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
 

Streaming lines from a gzip file in Rust

iterators io-streaming decompression
Intermediate 6 steps