Trait byte_num::convert::FromAscii [−][src]
pub trait FromAscii: Sized { fn bytes_to_int(s: &[u8]) -> Result<Self, ()>; fn atoi<S: AsRef<[u8]>>(s: S) -> Result<Self, ()> { ... } }
This trait allows convertion from bytes to integers.
Required Methods
fn bytes_to_int(s: &[u8]) -> Result<Self, ()>
Performs the actual conversion from a byteslice to an unsigned integer.
Provided Methods
fn atoi<S: AsRef<[u8]>>(s: S) -> Result<Self, ()>
The function performing the conversion. It takes anything that can be transformed into a byte-slice.
Examples
extern crate byte_num; use byte_num::convert::FromAscii; fn main() { assert_eq!(u32::atoi("1928"), Ok(1928)); assert_eq!(u32::atoi("12e3"), Err(())); }
Safety
It should be noted that trying to convert a string that does not fit in the chosen integer type, wraps around. For example:
extern crate byte_num; use byte_num::convert::FromAscii; fn main () { let n = u8::atoi("257"); assert_eq!(n, Ok(1)); }
Implementations on Foreign Types
impl FromAscii for u8[src]
impl FromAscii for u8impl FromAscii for u16[src]
impl FromAscii for u16impl FromAscii for u32[src]
impl FromAscii for u32impl FromAscii for u64[src]
impl FromAscii for u64impl FromAscii for usize[src]
impl FromAscii for usizeimpl FromAscii for i8[src]
impl FromAscii for i8impl FromAscii for i16[src]
impl FromAscii for i16impl FromAscii for i32[src]
impl FromAscii for i32impl FromAscii for i64[src]
impl FromAscii for i64impl FromAscii for isize[src]
impl FromAscii for isize