pub fn or<Input, P1, P2>(p1: P1, p2: P2) -> Or<P1, P2> where
Input: Stream,
P1: Parser<Input>,
P2: Parser<Input, Output = P1::Output>,
Expand description
Equivalent to p1.or(p2)
.
If you are looking to chain 3 or more parsers using or
you may consider using the
choice!
macro instead, which can be clearer and may result in a faster parser.
let mut parser = or(
string("let"),
or(digit().map(|_| "digit"), string("led")),
);
assert_eq!(parser.parse("let"), Ok(("let", "")));
assert_eq!(parser.parse("1"), Ok(("digit", "")));
assert!(parser.parse("led").is_err());
let mut parser2 = or(string("two"), string("three"));
// Fails as the parser for "two" consumes the first 't' before failing
assert!(parser2.parse("three").is_err());
// Use 'attempt' to make failing parsers always act as if they have not committed any input
let mut parser3 = or(attempt(string("two")), attempt(string("three")));
assert_eq!(parser3.parse("three"), Ok(("three", "")));