Why is Rust saying my variable is unused?

Shadowing a variable makes the previous variable inaccessible but it doesn't 'overwrite' it or similar.

So your original definition let myvar = 5; still exists, it's just not accessible anymore after your second definition myvar. However the original still is being tracked by the compiler, and it rightfully complains that you never use it.


I use it later in the println macro.

No you don’t. You are using the new variable myvar that’s shadowing the previous variable of the same name. That’s why the compiler correctly complains.

To fix this, remove the unnecessary let myvar = 5; declaration.

Tags:

Rust