Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Use A .map With A nom tag In Rust

This works

rust
//! ```cargo
//! [package]
//! edition = "2021"
//! [dependencies]
//! nom = { version = "7.1.3" }
//! ```


#![allow(unused_imports)]
use nom::bytes::complete::tag;
use nom::IResult;
use nom::Parser;

fn main() {
    assert_eq!(do_example("focus"), Ok(("", "asdf")));
    println!("Done.");
}

fn do_example(source: &str) -> IResult<&str, &str> {
    let (source, attr) = tag("focus")
         .parse(source)
         .map(|(x, _)| (x, "asdf"))?;
    Ok((source, attr))
}
results start

as opposed to this, which doesn't (and results in a [TODO: Code shorthand span ] error.

rust
pub fn attr_autofocus(source: &str) -> IResult<&str, Attribute> {
    let (source, attr) =
        tag("autofocus").map(|(x, y)| (x, Attribute::Autofocus))(source)?;
    Ok((source, attr))
}

Here's a look at the full error that I hit when trying to figure this out.

code full