rust
use std::borrow::Cow;

fn ensure_prefix(input: &str) -> Cow<str> {
    if input.starts_with("https://") {
        Cow::Borrowed(input)
    } else {

Cow for clone-on-write to avoid unnecessary allocations

rust optimization strings
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 std::collections::VecDeque;

fn main() {
    let mut queue = VecDeque::new();
    queue.push_back(1);
    queue.push_back(2);

VecDeque<T> for double-ended queue operations

rust collections
by Marcus Chen 1 tab
rust
use std::collections::HashSet;

fn main() {
    let mut words = HashSet::new();
    words.insert("hello");
    words.insert("world");

HashSet<T> for unique value collections

rust collections
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
fn main() {
    let mut numbers = Vec::new();
    numbers.push(1);
    numbers.push(2);
    numbers.push(3);

Vec<T> for growable arrays with owned data

rust collections
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
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
rust
struct UserId(u32);

impl From<u32> for UserId {
    fn from(id: u32) -> Self {
        UserId(id)
    }

From and Into for type conversions

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

struct Point {
    x: i32,
    y: i32,
}

std::fmt::Display for user-facing string representations

rust traits formatting
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
rust
use once_cell::sync::Lazy;
use regex::Regex;

static EMAIL_REGEX: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^[^ @]+@[^ @]+.[^ @]+$").unwrap()
});

once_cell for lazy static initialization

rust initialization patterns
by Marcus Chen 1 tab