Rust: Memory Safety and Performance for Systems Programming
Ownership System and Borrow Checker
Rust's killer feature: ownership (prevents memory bugs at compile time). Every value owned by single variable. Transfer ownership: let s1 = String::from("hello"); let s2 = s1; (s1 no longer owns string, s2 does). Attempt to use s1 → compile error (prevents use-after-free). Borrowing: let s1 = String::from("hello"); let len = calculate_length(&s1); (function borrows s1, doesn't own). Function: fn calculate_length(s: &String) -> usize { s.len() } (reference &String). After function returns, s1 still owns string. Mutable borrow: let mut s = String::from("hello"); change_string(&mut s); (allow modification, but only one mutable borrow at a time). Immutable borrows: multiple allowed (read-only). Lifetime annotations: fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str (returned reference valid as long as both inputs). Compiler prevents dangling pointers (reference to deallocated memory). Move semantics: efficient (copy vs move, decisions made at compile time). RAII: Resource Acquisition Is Initialization (files closed automatically when dropped).
Pattern Matching and Enum Handling
Match expressions: exhaustive checking. enum Result
Traits and Polymorphism
Traits: define shared behavior. trait Draw { fn draw(&self); }. Implementers: impl Draw for Circle { fn draw(&self) { println!("Drawing circle") } }. Trait objects: dyn Draw (any type implementing Draw). Vectors of traits: let shapes: Vec
Error Handling and Result Type
Result type: enum Result
Concurrency and Async/Await
Threads: std::thread::spawn(|| { ... }) (each thread owns data, compiler ensures thread-safe access). Channels: mpsc (multi-producer, single-consumer). let (tx, rx) = mpsc::channel(); spawn thread sends: tx.send("message"), main thread receives: rx.recv(). Sends ownership of message (original thread can't use). Arc
Performance and Systems Programming
Zero-cost abstractions: high-level syntax compiled to efficient machine code. Example: iterator for item in items { ... } compiles to loop (no allocation, comparable to C). SIMD support: portable_simd crate provides vector operations (data parallelism). Inline optimization: #[inline] hint, compiler inlines functions (reduces function call overhead). Unsafe code: unsafe { ... } allows raw pointers, manual memory management (opt-in). Use rarely (encapsulate in safe abstractions). FFI (Foreign Function Interface): call C libraries from Rust. extern "C" { fn c_function() -> i32; }. Performance: Rust ≈ C++ speed (both near-native). Benchmarks: quicksort: Rust 100ms, Go 150ms, Java 200ms, Python 5000ms (100x difference). Memory: Rust program ~10MB (runtime), Go ~50MB (runtime), Java ~100MB (JVM). Binary size: Rust ~5MB (hello world), Go ~2MB, Java requires JRE. Embedded systems: Rust popular (ARM, RISCV targets, resource-constrained).
Ecosystem and Production Use
Cargo: package manager. Cargo.toml: define dependencies, version constraints. cargo build (release optimized version). Clippy: linter detecting common mistakes. rustfmt: code formatter (standard style). Frameworks: Tokio (async runtime), Actix (web framework), Diesel (ORM), Tonic (gRPC). Web: Axum framework (minimal, ~50KB binary). Servers: high throughput (10K requests/second per core). Testing: #[cfg(test)], #[test] attribute. Tests run in parallel by default. Documentation: /// comments generate docs (cargo doc opens HTML). Community: Rust forums, Discord, GitHub. Adoption: Linux kernel (6.1+ has Rust support), Google (Android components rewritten in Rust), Microsoft (Windows components in Rust). Job market: growing (30-50% salary premium vs JavaScript). Learning curve: steep (ownership, lifetimes take time), but payoff (fewer runtime bugs).
Debugging and Development Workflow
Error messages: detailed, educational. Example: error[E0382]: value used after move (message explains, suggests fix). Compiler feedback: iterate fast (quick compilation). Development mode: cargo build (debug info, no optimization). Production: cargo build --release (optimizations, 5-10x faster, ~1 minute build time). Debugging: gdb/lldb work (Rust supports standard debuggers). VS Code Rust Analyzer: real-time type checking (similar to TypeScript). Profiling: cargo build --release, perf record (Linux), Instruments (macOS). Flame graphs: visualize execution time distribution. Memory profiling: Valgrind (detect memory leaks, though Rust has none). Static analysis: cargo clippy (catch style issues). CI/CD: cargo test (runs all tests), cargo clippy (lint check), cargo fmt --check (format check). Deployment: single binary (easy containerization). Startup: 1-10ms (fast startup, suitable for serverless).