I am making the guessing game form the book and when I compare the secret_number and guess var it gives me mismatched types even though that's what the book says, the 2 types are a string and a integer. Any ideas?
Can you please paste the code you have written, and the exact output from the compiler?
The program from the book declares two guess
variables. First, this guess
which is a String
:
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
But later on, it creates a new variable that is a u32
(a 32-bit unsigned integer):
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
When the comparison happens, guess
refers to this most-recently declared variable, not the earlier one. (The earlier binding is “shadowed” by the later one, and is no longer visible.)
4 Likes
Yes, I fixed it I just didn't read the entire thing, I found it after a couple minutes, but thanks for the help anyway.