Function chomp::combinators::option [] [src]

pub fn option<I: Input, T, E, F>(i: I, f: F, default: T) -> ParseResult<I, T, E> where
    F: FnOnce(I) -> ParseResult<I, T, E>, 

Tries the parser f, on success it yields the parsed value, on failure default will be yielded instead.

Incomplete state is propagated. Backtracks on error.

use chomp::prelude::{U8Input, SimpleResult, parse_only, option, token};

fn f<I: U8Input>(i: I) -> SimpleResult<I, u8> {
    option(i, |i| token(i, b'a'), b'd')
}

assert_eq!(parse_only(f, b"abc"), Ok(b'a'));
assert_eq!(parse_only(f, b"bbc"), Ok(b'd'));