Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Use .map With tuple() In The nom Parser In Rust

This returns a `&str`` instead of a `tuple``

Code

use nom::bytes::complete::tag;
use nom::character::complete::multispace1;
use nom::sequence::tuple;
use nom::IResult;

fn main() {
    let (a, b) = parse("alfa bravo charlie").unwrap();
    dbg!(a);
    dbg!(b);
}

fn parse(source: &str) -> IResult<&str, &str> {
    let (a, b) = tuple((
        tag("alfa"), 
        multispace1, 
        tag("bravo"))
    )(source)
        .map(|(x, y)| (x, y.2))?;
    Ok((a, b))
}

References