Function atoi::atoi
[−]
[src]
pub fn atoi<I>(text: &[u8]) -> (I, usize) where
I: Zero + One + AddAssign + MulAssign,
Parses an integer from a slice.
Contrary to its 'C' counterpart atoi is generic and will require a type argument if the type inference can not determine its result.
Example
use atoi::atoi; // Parsing to digits from a slice assert_eq!((42,2), atoi::<u32>(b"42")); // Additional bytes after the number are ignored assert_eq!((42,2), atoi::<u32>(b"42 is the answer to life, the universe and everything")); // (0,0) is returned if the slice does not start with a digit assert_eq!((0,0), atoi::<u32>(b"Sadly we do not know the question")); // While signed integer types are supported... assert_eq!((42,2), atoi::<i32>(b"42")); // ... signs currently are not (subject to change in future versions) assert_eq!((0,0), atoi::<i32>(b"-42"));
Return
Returns a tuple with two numbers. The first is the integer parsed or zero, the second is the index of the byte right after the parsed number. If the second element is zero the slice did not start with an ASCII digit.