rust

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 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 std::mem;

fn main() {
    let mut x = 5;
    let mut y = 10;

std::mem helpers for low-level memory manipulation

rust memory
by Marcus Chen 1 tab
rust
use std::rc::Rc;
use std::cell::RefCell;

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

Rc and RefCell for shared ownership with interior mutability

rust smart-pointers interior-mutability
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
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