Function combine::combinator::optional [] [src]

pub fn optional<P>(parser: P) -> Optional<P> where
    P: Parser

Parses parser and outputs Some(value) if it succeeds, None if it fails without consuming any input. Fails if parser fails after having consumed some input.

let mut parser = optional(string("hello"));
assert_eq!(parser.parse("hello"), Ok((Some("hello"), "")));
assert_eq!(parser.parse("world"), Ok((None, "world")));
assert!(parser.parse("heya").is_err());