Function opt
pub fn opt<Input, Output, Error, ParseNext>(
parser: ParseNext,
) -> impl Parser<Input, Option<Output>, Error>
Available on crate feature
dep_winnow
only.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)));