Function winnow::combinator::opt

source ·
pub fn opt<I: Stream, O, E: ParserError<I>, F>(
    f: F,
) -> impl Parser<I, Option<O>, E>
where F: Parser<I, O, E>,
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)));