I am quite new to rust, yet at the same time I'm itching to experiment with code.
I have studied the guessing game and I see that for each failed input the loop is performed again. I want to prevent this, and have the retry message triggered by the < Err(_) > method of the code.
fn reading() -> u32 {
println! ("insert number");
let mut a = String::new();
loop {
io::stdin() .read_line(&mut a) .expect("");
let a: u32 = match a.trim().parse(){
Ok(num) => num,
Err(_) => continue, // insert println! (retry message) here
};
break a;
}
}
There's actually still a bug in the program though, which has to do with how read_line works. You should see if you can figure out what's going on and how you could fix it.
Hello & Thank you for the explanation. The information I was after was was the block you mentioned at the very end. I can see there is an error when running it; I think this error plagued my code when I tried trying something similar.