I just started to experiment with Rust, so I've implemented modified version from Book's "Guessing game". Every first time I'm trying to enter the number, program fails to parse integer from string:
Guess the number!
Input your guess:
50
You guessed 50
(4 bytes)!
thread 'main' panicked at 'Wrong number format!: ParseIntError { kind: InvalidDigit }', src\libcore\result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: process didn't exit successfully: `target\release\experiment.exe` (exit code: 101)
Code applied below.
I already tried other integer types.
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!\nInput your guess:");
let mut guess = String::new();
let secnum = rand::thread_rng().gen_range(1,101);
'br: loop {
match io::stdin().read_line(&mut guess) {
Ok(okay) => {
println!("You guessed {} ({} bytes)!", guess, okay);
let intguess = guess.parse::<u8>()
.expect("Wrong number format!");
match secnum.cmp(&intguess) {
std::cmp::Ordering::Less => {
println!("Your guess is smaller!");
}
std::cmp::Ordering::Greater => {
println!("Your guess is bigger!");
}
std::cmp::Ordering::Equal => {
println!("You guessed right!");
break 'br;
}
}
}
Err(erro) => {
println!("Failed to read the line: {}!", erro);
}
}
}
}