Function btoi::btoi [] [src]

pub fn btoi<I>(bytes: &[u8]) -> Result<I, ParseIntegerError> where
    I: FromPrimitive + Zero + CheckedAdd + CheckedSub + CheckedMul

Converts a byte slice to an integer.

Like btou, but numbers may optionally start with a sign (- or +).

Errors

Returns ParseIntegerError for any of the following conditions:

  • bytes has no digits
  • not all characters of bytes are 0-9, excluding an optional leading sign
  • the number overflows or underflows I

Panics

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

Examples

assert_eq!(Ok(123), btoi(b"123"));
assert_eq!(Ok(123), btoi(b"+123"));
assert_eq!(Ok(-123), btoi(b"-123"));

assert!(btoi::<i16>(b"123456789").is_err()); // overflow
assert!(btoi::<u32>(b"-1").is_err()); // underflow

assert!(btoi::<i32>(b" 42").is_err()); // leading space