Function cut

fn cut<I, E: ParseError<I>, F>(parser: F) -> impl Parser<I, Output = <F as Parser<I>>::Output, Error = E>
where
    F: Parser<I, Error = E>

Transforms an Err::Error (recoverable) to Err::Failure (unrecoverable)

This commits the parse result, preventing alternative branch paths like with [nom::branch::alt][crate::branch::alt].

Example

Without cut:

# use nom::{Err,error::ErrorKind, IResult, Parser};
# use nom::character::complete::{one_of, digit1};
# use nom::combinator::rest;
# use nom::branch::alt;
# use nom::sequence::preceded;
# fn main() {

fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of("+-"), digit1),
    rest
  )).parse(input)
}

assert_eq!(parser("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser("ab"), Ok(("", "ab")));
assert_eq!(parser("+"), Ok(("", "+")));
# }

With cut:

# use nom::{Err,error::ErrorKind, IResult, Parser, error::Error};
# use nom::character::complete::{one_of, digit1};
# use nom::combinator::rest;
# use nom::branch::alt;
# use nom::sequence::preceded;
use nom::combinator::cut;
# fn main() {

fn parser(input: &str) -> IResult<&str, &str> {
  alt((
    preceded(one_of("+-"), cut(digit1)),
    rest
  )).parse(input)
}

assert_eq!(parser("+10 ab"), Ok((" ab", "10")));
assert_eq!(parser("ab"), Ok(("", "ab")));
assert_eq!(parser("+"), Err(Err::Failure(Error { input: "", code: ErrorKind::Digit })));
# }