Declaration and redeclaration

Hello,
In the following code when a redeclaration happened, then n is a new variable in the memory?

fn main() {
    let mut n = 1;      // Declaration
    print!("{}", n);
    n = 2;
    print!(" {}", n);
    let n = 3.14;        // Redeclaration
    print!(" {}", n);
    }

The old variable is no longer accessible and hasn’t been destroyed?

Thank you.

Yes.

2 Likes

The optimizer is capable of figuring out that the variable's storage may be reclaimed sooner. It will never do this in a way that changes the behavior of the program, though.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.