Thread 'main' panicked at 'Not an Integer!: ParseIntError { kind: InvalidDigit }'

 let mut user_input = String::new();
 io::stdin().read_line(&mut user_input).expect("Wrong Number");

 user_input = user_input.trim().to_string();

 let mut distance_b:Vec<i32> = user_input.split(" ")
    									    .map(|x| x.parse::<i32>().expect("Not an Integer!"))
    										.collect();

Trying to take signed integers as input from user

Getting this output when user enters negative integer as the first digit
thread 'main' panicked at 'Not an Integer!: ParseIntError { kind: InvalidDigit }', src/libcore/result.rs:859

Most likely you are hitting the problem due to wrong whitespace handling. If you are handling unsanitized user input then split_whitespace will be a better idea (handles other whitespace than space), or you would need to filter out empty "" resulting from split(" ") when there are more than one whitespace between the digit.

  let dist: Vec<_> = "  -4    66 -77  88  "
        .trim()
        .split_whitespace()
        .map(|x| x.parse::<i32>().expect("Not an Integer!"))
        .collect();
1 Like