rust
fn divide(a: i32, b: i32) -> Option<i32> {
    if b == 0 {
        None
    } else {
        Some(a / b)
    }

Option<T> for explicit null handling

rust option null-safety
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
rust
use std::fmt::Display;

fn print_it<T: Display>(value: T) {
    println!("Value: {}", value);
}

Trait bounds for generic functions with behavior constraints

rust traits generics
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
use tokio::time::{sleep, Duration};

async fn fetch_data() -> String {
    sleep(Duration::from_millis(100)).await;
    "data".to_string()
}

async/await with tokio for concurrent I/O without blocking threads

rust async tokio
by Marcus Chen 1 tab
rust
use std::sync::mpsc;
use std::thread;

fn main() {
    let (tx, rx) = mpsc::channel();

Channels (mpsc) for message passing between threads

rust concurrency channels
by Marcus Chen 1 tab
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
struct Config<'a> {
    name: &'a str,
    value: &'a str,
}

fn parse_config(line: &str) -> Config {

Lifetime annotations for flexible borrowing in structs

rust lifetimes borrowing
by Marcus Chen 1 tab
rust
fn calculate_length(s: &String) -> usize {
    s.len()
}

fn append_suffix(s: &mut String) {
    s.push_str(" world");

Borrowing with & and &mut for zero-cost access

rust ownership borrowing
by Marcus Chen 1 tab
rust
fn take_ownership(s: String) {
    println!("Took ownership: {}", s);
} // s is dropped here

fn main() {
    let message = String::from("hello");

Ownership transfer prevents double-free and use-after-free

rust ownership memory-safety
by Marcus Chen 1 tab
rust
use anyhow::{Context, Result};
use std::fs;

fn load_config(path: &str) -> Result<String> {
    fs::read_to_string(path)
        .with_context(|| format!("failed to read config from {}", path))

anyhow::Context for adding error context without custom types

rust error-handling cli
by Marcus Chen 1 tab
rust
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ConfigError {
    #[error("file not found: {0}")]
    NotFound(String),

Custom error types with thiserror for domain errors

rust error-handling libraries
by Marcus Chen 1 tab