Function chumsky::text::digits

source ·
pub fn digits<C: Character, E: Error<C>>(
    radix: u32
) -> impl Parser<C, C::Collection, Error = E> + Copy + Clone
Expand description

A parser that accepts one or more ASCII digits.

The output type of this parser is Character::Collection (i.e: String when C is char, and Vec<u8> when C is u8).

The radix parameter functions identically to char::is_digit. If in doubt, choose 10.

Examples

let digits = text::digits::<_, Simple<char>>(10);

assert_eq!(digits.parse("0"), Ok("0".to_string()));
assert_eq!(digits.parse("1"), Ok("1".to_string()));
assert_eq!(digits.parse("01234"), Ok("01234".to_string()));
assert_eq!(digits.parse("98345"), Ok("98345".to_string()));
// A string of zeroes is still valid. Use `int` if this is not desirable.
assert_eq!(digits.parse("0000"), Ok("0000".to_string()));
assert!(digits.parse("").is_err());