Function nom::multi::count

source · []
pub fn count<I, O, E, F>(
    f: F,
    count: usize
) -> impl FnMut(I) -> IResult<I, Vec<O>, E> where
    I: Clone + PartialEq,
    F: Parser<I, O, E>,
    E: ParseError<I>, 
Expand description

Runs the embedded parser a specified number of times. Returns the results in a Vec.

Arguments

  • f The parser to apply.
  • count How often to apply the parser.
use nom::multi::count;
use nom::bytes::complete::tag;

fn parser(s: &str) -> IResult<&str, Vec<&str>> {
  count(tag("abc"), 2)(s)
}

assert_eq!(parser("abcabc"), Ok(("", vec!["abc", "abc"])));
assert_eq!(parser("abc123"), Err(Err::Error(Error::new("123", ErrorKind::Tag))));
assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Tag))));
assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Tag))));
assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));