rust

rust
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

Arc and Mutex for safe shared mutable state across threads

rust concurrency threading
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
toml
[features]
default = ["json"]
json = ["serde_json"]
yaml = ["serde_yaml"]

[dependencies]

Feature flags for conditional compilation

rust cargo features
by Marcus Chen 2 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Config {
    host: String,
    port: u16,

serde for zero-copy serialization and deserialization

rust serde serialization
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 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 sqlx::PgPool;

#[derive(sqlx::FromRow)]
struct User {
    id: i32,
    name: String,

sqlx for compile-time checked SQL queries with async

rust database sql
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 tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        sleep(Duration::from_millis(100)).await;

tokio::spawn for concurrent task execution

rust async tokio
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
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