Code Explainers

Rust code explainers

rust
pub fn collapse_whitespace(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut in_whitespace = false;
 

Collapsing runs of whitespace in Rust

string-processing state-machine iteration
Beginner 5 steps
rust
use axum::{
    http::{header, StatusCode},
    response::{IntoResponse, Response},
    Json,

Turning errors into RFC 7807 responses in Axum

error-handling enums trait-implementation
Intermediate 7 steps
rust
use serde::{Deserialize, Serialize};
 
#[derive(Debug, Serialize, Deserialize)]
pub struct UserProfile {

Trimming JSON output with serde attributes

serialization json attributes
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::State,
    http::{header, StatusCode},

Streaming NDJSON from Postgres in Axum

streaming backpressure async
Advanced 9 steps
rust
use std::net::{IpAddr, SocketAddr};
 
use axum::extract::{ConnectInfo, FromRequestParts};
use axum::http::request::Parts;

How a custom ClientIp extractor works in Axum

extractor http-headers proxy-forwarding
Intermediate 8 steps
rust
use once_cell::sync::Lazy;
use regex::Regex;
 
static LOG_LINE: Lazy<Regex> = Lazy::new(|| {

Parsing access logs with a lazy regex in Rust

regex lazy-initialization parsing
Intermediate 7 steps
rust
use axum::{
    extract::State,
    response::sse::{Event, KeepAlive, Sse},
    Json,

Proxying an SSE chat stream in Axum

server-sent-events streaming async-generators
Advanced 10 steps
rust
use std::collections::HashMap;
 
fn decode_component(input: &str) -> String {
    let bytes = input.as_bytes();

Parsing a URL query string in Rust

url-encoding byte-parsing iterators
Intermediate 8 steps
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileKind {
    Png,
    Jpeg,

Detecting file types by magic bytes in Rust

pattern-matching byte-slices lookup-table
Intermediate 7 steps
rust
use crossbeam_channel::{Receiver, Sender, select, tick};
use std::time::Duration;
 
pub enum Command {

A channel-driven worker loop in Rust

channels select message-passing
Intermediate 9 steps
rust
use std::time::Duration;
 
use axum::{
    http::{header, HeaderValue, Request},

Serving fingerprinted assets in Axum

static-assets http-caching middleware
Intermediate 7 steps
rust
use std::collections::VecDeque;
 
#[derive(Debug)]
pub struct Hunk {

Applying a diff hunk in Rust

enums error-handling pattern-matching
Intermediate 8 steps
rust
use axum::{
    body::Body,
    http::{Method, StatusCode, Uri},
    response::{IntoResponse, Response},

Nesting routers and JSON fallbacks in Axum

routing http json-responses
Intermediate 7 steps
rust
use axum::{
    extract::FromRequestParts,
    http::{request::Parts, StatusCode, header::ACCEPT_LANGUAGE},
};

A locale extractor for Axum handlers

content-negotiation http-headers custom-extractor
Intermediate 7 steps
rust
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

Graceful thread shutdown with an atomic flag

concurrency atomics memory-ordering
Advanced 7 steps
rust
#[derive(Debug, PartialEq)]
enum State {
    FieldStart,
    InUnquoted,

Parsing a CSV line with a state machine

state machine parsing enums
Intermediate 9 steps
rust
use std::convert::TryFrom;
 
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpStatus {

Converting HTTP codes with TryFrom in Rust

enums error-handling trait-implementation
Intermediate 8 steps
rust
use axum::{
    extract::{Request, State},
    http::{HeaderValue, StatusCode},
    middleware::Next,

API version headers with Axum middleware

middleware http-headers versioning
Intermediate 8 steps