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
mod utils {
    pub fn helper() {
        println!("Helper function");
    }

    fn private_fn() {}

mod and pub for code organization and visibility

rust modules organization
by Marcus Chen 1 tab
rust
use std::env;

fn main() {
    let port: u16 = env::var("PORT")
        .unwrap_or_else(|_| "8080".to_string())
        .parse()

Environment variables with std::env for configuration

rust config environment
by Marcus Chen 1 tab
rust
use std::fs::File;
use std::io::{BufRead, BufReader, Result};

fn main() -> Result<()> {
    let file = File::open("input.txt")?;
    let reader = BufReader::new(file);

BufReader and BufWriter for efficient I/O buffering

rust io performance
by Marcus Chen 1 tab
rust
use std::fs;
use std::io::Result;

fn main() -> Result<()> {
    let contents = fs::read_to_string("input.txt")?;
    println!("File contents: {}", contents);

File I/O with std::fs for reading and writing files

rust io files
by Marcus Chen 1 tab
rust
use std::borrow::Cow;

fn ensure_prefix(input: &str) -> Cow<str> {
    if input.starts_with("https://") {
        Cow::Borrowed(input)
    } else {

Cow for clone-on-write to avoid unnecessary allocations

rust optimization strings
by Marcus Chen 1 tab
rust
fn greet(name: &str) {
    println!("Hello, {}", name);
}

fn main() {
    let owned = String::from("Alice");

String vs &str for owned vs borrowed text

rust strings
by Marcus Chen 1 tab
rust
use std::collections::VecDeque;

fn main() {
    let mut queue = VecDeque::new();
    queue.push_back(1);
    queue.push_back(2);

VecDeque<T> for double-ended queue operations

rust collections
by Marcus Chen 1 tab
rust
use std::collections::HashSet;

fn main() {
    let mut words = HashSet::new();
    words.insert("hello");
    words.insert("world");

HashSet<T> for unique value collections

rust collections
by Marcus Chen 1 tab
rust
use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert("Alice", 10);
    scores.insert("Bob", 20);

HashMap<K, V> for key-value lookups

rust collections
by Marcus Chen 1 tab
rust
fn main() {
    let mut numbers = Vec::new();
    numbers.push(1);
    numbers.push(2);
    numbers.push(3);

Vec<T> for growable arrays with owned data

rust collections
by Marcus Chen 1 tab
rust
#[derive(Default, Debug)]
struct Config {
    host: String,
    port: u16,
    debug: bool,
}

Default trait for sensible zero values

rust traits
by Marcus Chen 1 tab
rust
struct Logger {
    name: String,
}

impl Drop for Logger {
    fn drop(&mut self) {

Drop trait for custom cleanup logic

rust traits memory-management
by Marcus Chen 1 tab