Function digit1

fn digit1<T, E: ParseError<T>>(input: T) -> crate::internal::IResult<T, T, E>
where
    T: Input,
    <T as Input>::Item: AsChar

Recognizes one or more ASCII numerical characters: 0-9

Complete version: Will return an error if there's not enough input data, or the whole input if no terminating token is found (a non digit character).

Example

# use nom::{Err, error::{Error, ErrorKind}, IResult, Needed};
# use nom::character::complete::digit1;
fn parser(input: &str) -> IResult<&str, &str> {
    digit1(input)
}

assert_eq!(parser("21c"), Ok(("c", "21")));
assert_eq!(parser("c1"), Err(Err::Error(Error::new("c1", ErrorKind::Digit))));
assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Digit))));

Parsing an integer

You can use digit1 in combination with map_res to parse an integer:

# use nom::{Err, error::{Error, ErrorKind}, IResult, Needed, Parser};
# use nom::combinator::map_res;
# use nom::character::complete::digit1;
fn parser(input: &str) -> IResult<&str, u32> {
  map_res(digit1, str::parse).parse(input)
}

assert_eq!(parser("416"), Ok(("", 416)));
assert_eq!(parser("12b"), Ok(("b", 12)));
assert!(parser("b").is_err());