pub fn digit1<Input, Error>(
input: &mut Input,
) -> PResult<<Input as Stream>::Slice, Error>
Expand description
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).
[Partial version][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_))
if there’s not enough input data,
or if no terminating token is found (a non digit character).
§Effective Signature
Assuming you are parsing a &str
Stream:
pub fn digit1<'i>(input: &mut &'i str) -> PResult<&'i str>
§Example
fn parser<'s>(input: &mut &'s str) -> PResult<&'s str, InputError<&'s str>> {
digit1.parse_next(input)
}
assert_eq!(parser.parse_peek("21c"), Ok(("c", "21")));
assert_eq!(parser.parse_peek("c1"), Err(ErrMode::Backtrack(InputError::new("c1", ErrorKind::Slice))));
assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Slice))));
assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("21c")), Ok((Partial::new("c"), "21")));
assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("c1")), Err(ErrMode::Backtrack(InputError::new(Partial::new("c1"), ErrorKind::Slice))));
assert_eq!(digit1::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1))));
§Parsing an integer
You can use digit1
in combination with Parser::try_map
to parse an integer:
fn parser<'s>(input: &mut &'s str) -> PResult<u32, InputError<&'s str>> {
digit1.try_map(str::parse).parse_next(input)
}
assert_eq!(parser.parse_peek("416"), Ok(("", 416)));
assert_eq!(parser.parse_peek("12b"), Ok(("b", 12)));
assert!(parser.parse_peek("b").is_err());