rust
15 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use nom::{
bytes::complete::tag,
character::complete::digit1,
IResult,
};
fn parse_version(input: &str) -> IResult<&str, &str> {
let (input, _) = tag("v")(input)?;
digit1(input)
}
fn main() {
let result = parse_version("v123");
println!("{:?}", result);
}
1 file · rust
Explain with highlit
Nom is a parser combinator library for building parsers from small, composable functions. It's byte-oriented and zero-copy, making it ideal for binary protocols, config files, or log parsing. I define parsers for tokens (like tag("GET") or digit1), then combine them with map, tuple, alt, etc. The IResult type represents parse success/failure with remaining input. Nom parsers are pure functions, so they're easy to test and reuse. For text parsing, nom is faster than regex in many cases. The learning curve is steeper than regex, but the composability and performance are worth it for complex grammars.
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
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
Share this code
Here's the card — post it anywhere.