Function winnow::combinator::opt

source ·
pub fn opt<I: Stream, O, E: ParseError<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::character::alpha1;

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

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