Shadowing and mutability

Hey everyone, I'm new to the rust community and this may seem really silly but I dont quite get why shadowed version of guess (u32 parsed from string) isn't declared as a mutable variable. I find this a little confusing since we're repeatedly reassigning to the variable inside the loop.
However, when I do make it mutable, compiler tells me it needn't be mutable as a warning. Why is this so?

fn main() {
    println!("Guess The Number");
    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("Secret number is {}", secret_number);

    loop {
        println!("Please input your guess");

        let mut guess = String::new();
        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Not an integer, input again");
                continue;
            }
        };
        println!("You guessed {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("{}", "Too Small".red()),
            Ordering::Greater => println!("{}", "Too Big".red()),
            Ordering::Equal => {
                println!("{}", "You Win!".green());
                break;
            }
        }
    }
}

You can think of it as let always starting from scratch:

let mut x = 0;

// Yes, x is shadowed, but it's shadowed by a completely new x
// which has nothing to do with the old one, beyond the name
// overlap.
// Notice that a type change is done -- this is because x isn't
// *changed*, it is a completely new one.
let x = "hello";
1 Like

There is no reassignment; only redeclaration. Think of each loop iteration as having (two) separate guess variables from previous iterations, and the integer one is never mutated; it is initialized and then read.

(The compiler only processes them once, but the language behaves as if that's what you get.)

2 Likes