Skip to content

How to peek and, on success, consume one char only? #372

Answered by epage
ckoehler asked this question in Q&A
Discussion options

You must be logged in to vote

peek will backtrack on lack of success, so you should be able to do

pub fn my_parser<'s>(input: &mut &'s str) -> PResult<char> {
  let _ = peek("foo").parse_next(input)?;
  any.parse_next(input)
}

When foo doesn't match, you can do other things like opt, alt, etc.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ckoehler
Comment options

Answer selected by ckoehler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants