Code Explainers

Rust code explainers

rust
use serde::{Deserialize, Serialize};
 
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]

Tagged enums with serde in Rust

enums serialization json
Intermediate 6 steps
rust
use axum::{
    async_trait,
    extract::{FromRequestParts, State},
    http::{header, request::Parts, StatusCode},

Custom auth extractors in Axum

extractors authentication error-handling
Intermediate 9 steps
rust
use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;
 

Debouncing filesystem events in async Rust

debounce async channels
Advanced 7 steps
rust
pub fn merge_intervals(mut intervals: Vec<(i64, i64)>) -> Vec<(i64, i64)> {
    if intervals.is_empty() {
        return Vec::new();
    }

Merging overlapping intervals in Rust

intervals sorting greedy
Intermediate 7 steps
rust
use std::fmt;
use std::str::FromStr;
 
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

A validated Email newtype in Rust

newtype validation error-handling
Intermediate 9 steps
rust
use std::time::Duration;
 
use axum::{
    body::Body,

Structured request tracing in Axum

middleware observability tracing
Intermediate 7 steps
rust
pub struct ScopeGuard<F: FnMut()> {
    rollback: F,
    active: bool,
}

A RAII rollback guard in Rust

raii error-handling closures
Advanced 8 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json};
use serde::Serialize;
use std::sync::Arc;
 

Concurrent dashboard aggregation in Axum

concurrency error-handling graceful-degradation
Intermediate 7 steps
rust
use axum::{
    extract::{Path, State},
    http::{header, HeaderMap, StatusCode},
    response::{IntoResponse, Response},

Optimistic concurrency with ETags in Axum

optimistic-concurrency etag http-headers
Intermediate 8 steps
rust
use std::fmt;
 
#[derive(Debug)]
pub enum ConfigError {

Building a custom error type in Rust

error-handling enums trait-implementation
Intermediate 9 steps
rust
use axum::{
    async_trait,
    extract::{rejection::JsonRejection, FromRequest, Request},
    http::StatusCode,

A validating JSON extractor in Axum

extractors validation error-handling
Intermediate 7 steps
rust
use std::fmt;
 
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Money {

Formatting money with Rust's Display trait

trait-implementation integer-arithmetic enums
Intermediate 10 steps
rust
use std::collections::HashSet;
use std::io::{self, BufRead, BufWriter, Write};
 
fn main() -> io::Result<()> {

Filtering duplicate lines in Rust

stdin hashset buffering
Beginner 5 steps
rust
use axum::{extract::{Query, State}, http::StatusCode, Json};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;

Cursor pagination in an Axum handler

cursor-pagination keyset-pagination base64
Advanced 10 steps
rust
use std::num::ParseIntError;
 
fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> {
    line.split(',')

Collecting Results in Rust iterators

iterators error-handling result
Intermediate 7 steps
rust
use std::sync::Arc;
 
use axum::{
    extract::State,

Offloading work with a channel in Axum

background-jobs message-passing shared-state
Intermediate 9 steps
rust
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Instant;
 
pub struct TimedGuard<'a, T> {

A self-timing mutex guard in Rust

raii mutex deref
Advanced 7 steps
rust
use std::error::Error;
use std::path::Path;
 
use serde::Deserialize;

Custom CSV deserialization with serde in Rust

serde csv deserialization
Intermediate 7 steps