'Why is my program panicking although the datatype is same?
Why is my program generating
thread 'main' panicked at 'called
Result::unwrap()on anErrvalue: ParseIntError { kind: InvalidDigit }', src/main.rs:68:54 note: run withRUST_BACKTRACE=1environment variable to display a backtrace
when I enter the generated hash it panics instead of passing control to if {..}.
fn main() {
println!("Hello user! Please cast your input ");
let userinput: u64 = user_input();
let mut hasher_two = DefaultHasher::new();
let vecdata = [0x09, 0x06, 0xba, 0x67, 0x76];
hasher_two.write(&vecdata);
let final_check = hasher_two.finish();
if final_check == userinput {
correct_file_creation();
} else {
wrong_file_creation();
}
}
pub fn user_input() -> u64 {
let mut userinput = String::new();
std::io::stdin().read_line(&mut userinput).unwrap();
let userinputinteger: i32 = userinput.trim().parse().unwrap();
return userinputinteger as u64;
}
Solution 1:[1]
You need to be more specific, since you're not parsing a normal number.
You'll need to tell rust that you want to parse a hex string.
See this answer on how to do that:
Converting a hexadecimal string to a decimal integer
I do not know anything about rust but your error message suggests that because the error is in ParseIntError with a second clue in that error message: kind: InvalidDigit which means that it has no idea what an a or f is doing in your string that's supposed to be an integer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RedX |
