Marcus Chen

78 code snips · on codesnips 5 months

Rust systems engineer building high-performance tools and services. Deep focus on memory safety, async patterns, zero-cost abstractions, and production reliability. 10+ years in...

rust
struct UserId(u32);

impl From<u32> for UserId {
    fn from(id: u32) -> Self {
        UserId(id)
    }

From and Into for type conversions

rust traits conversion
by Marcus Chen 1 tab
rust
use std::fmt;

struct Point {
    x: i32,
    y: i32,
}

std::fmt::Display for user-facing string representations

rust traits formatting
by Marcus Chen 1 tab
toml
[package]
name = "my-app"
version = "0.1.0"

[dependencies]
tokio = "1"

Cargo.lock for reproducible builds

rust cargo dependencies
by Marcus Chen 1 tab
rust
use once_cell::sync::Lazy;
use regex::Regex;

static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^[^ @]+@[^ @]+.[^ @]+$").unwrap()
});

once_cell for lazy static initialization

rust initialization patterns
by Marcus Chen 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 tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let fast = sleep(Duration::from_millis(50));
    let slow = sleep(Duration::from_millis(200));

tokio::select! for racing multiple async operations

rust async tokio
by Marcus Chen 1 tab
rust
use std::pin::Pin;
use std::future::Future;

async fn example() {
    println!("Example future");
}

Pin and Unpin for safe self-referential async futures

rust async pin
by Marcus Chen 1 tab
rust
macro_rules! create_function {
    ($func_name:ident) => {
        fn $func_name() {
            println!("Called {}", stringify!($func_name));
        }
    };

Declarative macros (macro_rules!) for code generation

rust macros metaprogramming
by Marcus Chen 1 tab
rust
struct Locked;
struct Unlocked;

struct Door<State> {
    _state: std::marker::PhantomData<State>,
}

Type state pattern for compile-time state machines

rust patterns type-safety
by Marcus Chen 1 tab
rust
pub struct Server {
    host: String,
    port: u16,
    workers: usize,
}

Builder pattern for complex struct initialization

rust patterns builder
by Marcus Chen 1 tab
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct UserId(u32);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct PostId(u32);

Newtype pattern for type-safe primitives

rust type-safety patterns
by Marcus Chen 1 tab
rust
use rayon::prelude::*;

fn main() {
    let numbers: Vec<_> = (0..1000).collect();

    let sum: i32 = numbers

Rayon for data parallelism with par_iter

rust parallelism rayon
by Marcus Chen 1 tab