rust

rust
#[derive(Debug, Clone, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

Derive macros for automatic trait implementations

rust macros traits
by Marcus Chen 1 tab
toml
[workspace]
members = [
    "server",
    "client",
    "common",
]

Cargo workspaces for multi-crate projects

rust cargo workspace
by Marcus Chen 1 tab
rust
enum Status {
    Ok,
    Error(String),
    Pending,
}

Pattern matching with match for exhaustive case handling

rust pattern-matching
by Marcus Chen 1 tab
bash
# Install
cargo install cargo-expand

# Expand entire crate
cargo expand

cargo-expand to inspect macro expansions

rust macros debugging
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
use std::fs;
use std::num::ParseIntError;

fn read_port(path: &str) -> Result<u16, Box<dyn std::error::Error>> {
    let contents = fs::read_to_string(path)?;
    let port: u16 = contents.trim().parse()?;

Result and ? operator for clean error propagation

rust error-handling
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::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
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 sqlx::PgPool;
use std::time::Duration;
use tokio::sync::mpsc;

#[derive(Debug, Clone)]
pub struct MetricPoint {

Batching Database Writes in Rust with a Size- and Interval-Triggered Flush Buffer

rust tokio sqlx
by codesnips 3 tabs
rust
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use uuid::Uuid;

pub struct Id<T> {

Type-Safe Entity IDs in Rust with a Zero-Cost Id<T> Newtype

rust newtype type-safety
by codesnips 3 tabs
rust
use std::time::Duration;
use rand::Rng;

#[derive(Clone, Debug)]
pub struct BackoffPolicy {
    pub base_delay: Duration,

Exponential Backoff With Jitter for Retrying Fallible Async Operations in Rust

rust tokio async
by codesnips 3 tabs