Cogs and Levers A blog full of technical stuff

nom

Parsing is usually messy.

Manual string slicing. Index math. State machines.

nom approaches parsing differently:

Composable parser combinators.

What Problem Does nom Solve?

Building parsers using:

  • Small reusable functions
  • Functional composition
  • Zero-copy input slices

Without writing a giant state machine.

Minimal Example

Cargo.toml

[dependencies]
nom = "7"

main.rs

use nom::{
    bytes::complete::tag,
    character::complete::digit1,
    sequence::tuple,
    IResult,
};

fn parse(input: &str) -> IResult<&str, (&str, &str)> {
    tuple((tag("ID:"), digit1))(input)
}

fn main() {
    let result = parse("ID:12345");
    println!("{result:?}");
}

What’s Actually Happening?

Nom parsers:

  • Take input
  • Return (remaining_input, parsed_value)
  • Compose like functions

It’s functional parsing in Rust.

Should You Use It?

If you’re writing:

  • Binary protocol parsers
  • DSLs
  • Structured log parsers

Yes.

But be prepared to think functionally.