rust
// build.rs
fn main() {
    println!("cargo:rerun-if-changed=build.rs");

    let version = env!("CARGO_PKG_VERSION");
    println!("cargo:rustc-env=BUILD_VERSION={}", version);

Build scripts (build.rs) for compile-time code generation

rust cargo build
by Marcus Chen 1 tab
rust
use std::io::Write;
use tempfile::NamedTempFile;

fn main() -> std::io::Result<()> {
    let mut tmpfile = NamedTempFile::new()?;
    writeln!(tmpfile, "Hello, temp file!")?;

tempfile for safe temporary file creation

rust testing files
by Marcus Chen 1 tab
rust
use std::process::Command;

fn main() -> std::io::Result<()> {
    let output = Command::new("ls")
        .arg("-la")
        .output()?;

std::process::Command for spawning external processes

rust processes cli
by Marcus Chen 1 tab
rust
use std::path::Path;

fn process_file(path: impl AsRef<Path>) {
    let path = path.as_ref();
    println!("Processing: {}", path.display());
}

AsRef and AsMut for flexible function parameters

rust traits conversion
by Marcus Chen 1 tab
rust
use std::ops::Deref;

struct MyBox<T>(T);

impl<T> Deref for MyBox<T> {
    type Target = T;

Deref and DerefMut for smart pointer ergonomics

rust traits smart-pointers
by Marcus Chen 1 tab
rust
trait Container {
    type Item;
    fn get(&self, index: usize) -> Option<&Self::Item>;
}

struct Warehouse {

Associated types in traits for cleaner generics

rust traits generics
by Marcus Chen 1 tab
rust
use std::fmt::Display;

fn make_adder(x: i32) -> impl Fn(i32) -> i32 {
    move |y| x + y
}

impl Trait for opaque return types

rust traits
by Marcus Chen 1 tab
rust
const MAX_SIZE: usize = 1024;

const fn square(n: u32) -> u32 {
    n * n
}

const and const fn for compile-time evaluation

rust const performance
by Marcus Chen 1 tab
rust
fn print_size<T: ?Sized>(value: &T) {
    // Can accept both &str and &String
    println!("Value: {:p}", value as *const T as *const ());
}

fn main() {

Sized trait and ?Sized for dynamically-sized types

rust traits types
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 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 parking_lot::Mutex;
use std::sync::Arc;
use std::thread;

fn main() {
    let counter = Arc::new(Mutex::new(0));

Parking_lot for faster synchronization primitives

rust concurrency performance
by Marcus Chen 1 tab