rust
12 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
struct Config<'a> {
name: &'a str,
value: &'a str,
}
fn parse_config(line: &str) -> Config {
let parts: Vec<&str> = line.split('=').collect();
Config {
name: parts[0],
value: parts[1],
}
}
1 file · rust
Explain with highlit
When a struct holds a reference, you must annotate its lifetime so the compiler knows the reference won't outlive the data. The syntax <'a> declares a lifetime parameter, and &'a str ties the reference to that lifetime. This ensures that as long as the struct exists, the borrowed data is valid. Lifetimes can feel abstract at first, but they're just a way to express "this reference lives at least as long as that." I use them in parsers, config structs, and iterators where copying data would be wasteful. The compiler infers lifetimes in most function signatures, but structs require explicit annotations. Once you're comfortable with lifetimes, you can write zero-copy APIs that are both safe and fast.
Related snips
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
rust
macro_rules! create_function {
($func_name:ident) => {
fn $func_name() {
println!("Called {}", stringify!($func_name));
}
};
Declarative macros (macro_rules!) for code generation
rust
macros
metaprogramming
by Marcus Chen
1 tab
rust
use my_crate::add;
#[test]
fn test_public_api() {
assert_eq!(add(3, 4), 7);
}
Integration tests in tests/ directory
rust
testing
integration
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.