jweinst1
5/10/2019 - 8:23 PM

parses an int from a string in rust

parses an int from a string in rust

// parse an int from string and return number of chars parsed.
fn parse_int_from_str(text:&str) -> (u32, usize) {
    let mut chars_consumed = 0;
    for ch in text.chars() {
        if !ch.is_digit(10) {
            break;
        }
        chars_consumed += 1;
    }
    match text.parse::<u32>() {
        Ok(num) => (num, chars_consumed),
        Err(e) => panic!("Got err '{}' when parsing.", e)
    }
}


fn main() {
   let foo = parse_int_from_str("32");
   println!("{:?}", foo);
   let doo = parse_int_from_str("32e");
}