While Rust comes with strict static typing, surprisingly it supports the concept of Shadowing for types. Shadowing allows variable that was once declared as one type to be redeclared as a new type. In effect, shadowing or overwriting the previous declaration.
Let's take a quick look at Shadowing in Rust. Here's an example of the beginner's favorite guessing game example.
Take a look at line number 10 and 15, and observe that the variable
guess
is declared. First time as a mutable string, and then as an immutable unsigned 32-bit integer.
Shadowing lets us re-use the guess name, rather than forcing us to come up with two unique names like guess_str
and guess
, or something else.
Top comments (0)