Function btoi::btou

source ·
pub fn btou<I>(bytes: &[u8]) -> Result<I, ParseIntegerError>where
    I: FromPrimitive + Zero + CheckedAdd + CheckedMul,
Expand description

Converts a byte slice to an integer. Signs are not allowed.

Errors

Returns ParseIntegerError for any of the following conditions:

  • bytes is empty
  • not all characters of bytes are 0-9
  • the number overflows I

Panics

Panics in the pathological case that there is no representation of 10 in I.

Examples

assert_eq!(Ok(12345), btou(b"12345"));
assert!(btou::<u8>(b"+1").is_err()); // only btoi allows signs
assert!(btou::<u8>(b"256").is_err()); // overflow