Code Explainers

Rust code explainers

rust
use std::env;
use std::time::Duration;
 
#[derive(Debug, Clone)]

Loading typed config from environment variables in Rust

configuration error-handling generics
Intermediate 7 steps
rust
use std::time::Duration;
use tokio::time::sleep;
 
#[derive(Debug)]

Async retry with exponential backoff in Rust

retry exponential-backoff async
Intermediate 8 steps
rust
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
 

Recursive file search by extension in Rust

recursion filesystem error-handling
Intermediate 8 steps
rust
use axum::{
    extract::ws::{Message, WebSocket, WebSocketUpgrade},
    response::IntoResponse,
};

How an Axum WebSocket echo server works

websockets async streams protocol handling
Intermediate 8 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},

Typed error handling in an Axum handler

error-handling extractors validation
Intermediate 9 steps
rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
 
pub fn top_k<T: Ord + Clone>(items: &[T], k: usize) -> Vec<T> {

Top-K selection with a bounded min-heap in Rust

heap top-k generics
Intermediate 8 steps
rust
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
 

Modeling nested JSON with serde in Rust

deserialization json struct-mapping
Intermediate 9 steps
rust
use axum::{
    body::Bytes,
    extract::State,
    http::{HeaderMap, StatusCode},

Verifying Stripe webhook signatures in Axum

hmac webhooks constant-time-comparison
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::Request,
    http::{header, StatusCode},

How bearer-auth middleware works in Axum

middleware authentication request extensions
Intermediate 7 steps
rust
use std::borrow::Cow;
 
fn sanitize_filename(input: &str) -> Cow<'_, str> {
    if input.chars().all(|c| c.is_alphanumeric() || matches!(c, '.' | '-' | '_')) {

Avoiding allocations with Cow in Rust

clone-on-write borrowing zero-copy
Intermediate 7 steps
rust
use axum::{
    http::StatusCode,
    response::IntoResponse,
    routing::get,

Serving an SPA and API with Axum

routing static-files spa-fallback
Intermediate 7 steps
rust
use std::time::{Duration, Instant};
 
pub struct TokenBucket {
    capacity: f64,

A token bucket rate limiter in Rust

rate-limiting token-bucket lazy-refill
Intermediate 8 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    routing::get,

Building a JSON user API in Axum

routing shared-state json-serialization
Intermediate 8 steps
rust
use std::time::Duration;
 
use axum::{
    http::{header, HeaderValue, Method},

Configuring CORS on an Axum Router

cors middleware http-headers
Intermediate 7 steps
rust
use axum::{
    body::Bytes,
    extract::State,
    http::StatusCode,

Handling raw byte uploads in Axum

extractors shared-state request-limits
Intermediate 7 steps
rust
use axum::{
    extract::{Query, State},
    http::StatusCode,
    Json,

Paginated, filtered product listing in Axum

pagination query-parameters sql-filtering
Intermediate 8 steps
rust
use axum::{
    extract::FromRef,
    http::StatusCode,
    response::{IntoResponse, Redirect},

Signed cookie sessions in Axum

sessions cookies authentication
Intermediate 8 steps
rust
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,

How a custom error type maps to HTTP in Axum

error-handling enums trait-implementation
Intermediate 7 steps