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
fn print_size<T: ?Sized>(value: &T) {
    // Can accept both &str and &String
    println!("Value: {:p}", value as *const T as *const ());
}

fn main() {

Sized trait and ?Sized for dynamically-sized types

rust traits types
by Marcus Chen 1 tab
rust
use color_eyre::Result;

fn might_fail() -> Result<()> {
    Err(color_eyre::eyre::eyre!("Something went wrong"))
}

color-eyre for beautiful error reports with backtraces

rust error-handling cli
by Marcus Chen 1 tab
rust
use nom::{
    bytes::complete::tag,
    character::complete::digit1,
    IResult,
};

nom for parser combinators and zero-copy parsing

rust parsing nom
by Marcus Chen 1 tab
rust
use parking_lot::Mutex;
use std::sync::Arc;
use std::thread;

fn main() {
    let counter = Arc::new(Mutex::new(0));

Parking_lot for faster synchronization primitives

rust concurrency performance
by Marcus Chen 1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;

fn main() {
    let (tx, rx) = unbounded();

Crossbeam for advanced concurrent data structures

rust concurrency lock-free
by Marcus Chen 1 tab
rust
use log::{info, warn, error};

fn main() {
    env_logger::init();

    info!("Application started");

log crate facade for pluggable logging backends

rust logging
by Marcus Chen 1 tab
rust
fn divide(a: i32, b: i32) -> i32 {
    if b == 0 {
        panic!("division by zero");
    }
    a / b
}

panic! and unwinding for unrecoverable errors

rust error-handling
by Marcus Chen 1 tab
rust
use std::marker::PhantomData;

struct Token<'a> {
    _marker: PhantomData<&'a ()>,
}

PhantomData for zero-cost type-level markers

rust generics phantomdata
by Marcus Chen 1 tab
rust
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let data = Arc::new(Mutex::new(vec![1, 2, 3]));

Send and Sync traits for safe concurrency guarantees

rust concurrency traits
by Marcus Chen 1 tab
rust
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

wasm-bindgen for Rust to JavaScript interop in WebAssembly

rust wasm webassembly
by Marcus Chen 1 tab
rust
#![feature(asm)]

fn add_asm(a: u64, b: u64) -> u64 {
    let result: u64;
    unsafe {
        asm!(

Inline assembly for critical performance hotspots

rust assembly performance
by Marcus Chen 1 tab
rust
#[cfg(target_os = "linux")]
fn platform_specific() {
    println!("Running on Linux");
}

#[cfg(target_os = "windows")]

cfg attribute for conditional compilation

rust conditional-compilation
by Marcus Chen 1 tab