[][src]Function lexical::try_parse_digits

pub fn try_parse_digits<N: Aton, Bytes: AsRef<[u8]>>(
    bytes: Bytes,
    base: u8
) -> Result<N, usize>

High-level conversion of bytes to a number with a custom radix.

This function only returns a value if the entire string is successfully parsed. For an unchecked version of this function, use parse_digits.

  • bytes - Byte slice to convert to number.
  • base - Number of unique digits for the number (radix).

Examples

// String overloads
assert_eq!(lexical::try_parse_digits::<i32, _>(b"5", 10), Ok(5));
assert_eq!(lexical::try_parse_digits::<i32, _>(b"1a", 10), Err(1));
assert_eq!(lexical::try_parse_digits::<f32, _>(b"0", 10), Ok(0.0));

// Bytes overloads
assert_eq!(lexical::try_parse_digits::<i32, _>(b"5", 10), Ok(5));
assert_eq!(lexical::try_parse_digits::<i32, _>(b"1a", 10), Err(1));
assert_eq!(lexical::try_parse_digits::<f32, _>(b"0", 10), Ok(0.0));