Function winnow::combinator::opt

source ·
pub fn opt<Input: Stream, Output, Error, ParseNext>(
    parser: ParseNext
) -> impl Parser<Input, Option<Output>, Error>
where ParseNext: Parser<Input, Output, Error>, Error: ParserError<Input>,
Expand description

Apply a Parser, producing None on ErrMode::Backtrack.

To chain an error up, see cut_err.

Example

use winnow::combinator::opt;
use winnow::ascii::alpha1;

fn parser(i: &str) -> IResult<&str, Option<&str>> {
  opt(alpha1).parse_peek(i)
}

assert_eq!(parser("abcd;"), Ok((";", Some("abcd"))));
assert_eq!(parser("123;"), Ok(("123;", None)));