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

pub fn option<'a, I, T, E, F>(i: Input<'a, I>, f: F, default: T) -> ParseResult<'a, I, T, E> where I: 'a + Copy, F: FnOnce(Input<'a, I>) -> ParseResult<'a, 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::{Input, U8Result, parse_only, option, token};

fn f(i: Input<u8>) -> U8Result<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'));