rust
12 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use std::mem;
fn main() {
let mut x = 5;
let mut y = 10;
mem::swap(&mut x, &mut y);
println!("x: {}, y: {}", x, y);
let old = mem::replace(&mut x, 42);
println!("old: {}, new: {}", old, x);
}
1 file · rust
Explain with highlit
The std::mem module provides utilities for working with memory: size_of, align_of, swap, replace, take, drop, forget, and transmute. I use mem::swap to exchange values without cloning, mem::replace to take a value out of a mutable reference, and mem::take for Default types. mem::drop explicitly drops a value early, and mem::forget prevents drop (leaks). mem::transmute reinterprets bytes (extremely unsafe, avoid when possible). These functions are building blocks for unsafe code and performance optimizations. For example, mem::replace(&mut self.data, Vec::new()) takes ownership without cloning. Understanding std::mem is key to writing efficient Rust.
Related snips
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
ruby
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
rails
performance
streaming
by codesnips
3 tabs
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
rust
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
rust
observability
tracing
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 clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
#[arg(short, long)]
clap for CLI argument parsing with derive macros
rust
cli
clap
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.