Let's take a look at some of the common pitfalls with the keywords and Then, we will learn how by using . let mut . immutable != constant variable shadowing Getting started with Rust can be daunting. Rust is well-known for being a language. One of the ways in which Rust is safe is through type-safety. Rust is strongly typed and defaults to immutable values. safe The "let" Keyword The simplest way to create a new variable in Rust is by using the "let" keyword: () { my_num = ; ( , my_num); } fn main let 5 println! "{}" introduces a new variable into the current scope. By default new variables are immutable, which means they can't be reassigned. For example: let () { my_num = ; my_num = ; ( , my_num); } fn main let 5 6 println! "{}" fails to compile with the error: cannot assign twice to immutable variable In Rust the keyword "let" in Rust can confused devs coming from JavaScript. In JS "let" is used to declare mutable values. In Rust, "let" declares immutable values, which contributes to Rust being a safer language. #rustlang #rust Variable Shadowing - The Dark Side of "let" As we can see above, Rust's immutability offered by the keyword allows the compiler to ensure that a given variable can't be changed... kind of. The following does fail to compile: let not () { my_num = ; my_num = ; ( , my_num); } fn main let 5 let 6 println! "{}" We to declare a new variable with the same name, even all in the same scope. This doesn't mutate " ", it creates a new variable with a new spot in memory. The name " " now refers to the new variable, and the old variable is no longer accessible by its name. are allowed my_num my_num Variable shadowing also works in an inner scope. In the outer scope it is in a way the original variable remains "unshadowed": () { my_num = ; { my_num = ; ( , my_num); } ( , my_num); } fn main let 5 // start new scope let 6 println! "{}" println! "{}" prints: 6 5 Notice how the pointer to the new variable is completely different: () { my_num = ; ( , &my_num); my_num = ; ( , &my_num); } fn main let 5 println! "my_num pointer address: {:p}" let 6 println! "my_num pointer address: {:p}" prints: my_num pointer : x7ffeee0ad6f4 my_num pointer : x7ffeee0ad74c address 0 address 0 I'm personally not yet a fan of variable shadowing. My first impression is that it ruins the absolute safety that could have been provided. That said, I've heard compelling arguments for why it should exist. Namely that creating a new variable with the same name is terribly convenient. tl;dr: Even though variables declared with "let" are immutable, the name of the variable can easily point to an entirely new variable. Don't count on it being a true constant. Mut - A "Normal" Mutable Variable Variables declared with "let" can optionally be declared mutable using the " " keyword: mut () { my_num = ; my_num = ; ( , my_num); } fn main let mut 5 6 println! "{}" Prints 6 Mutable variables are just that - mutable. The value changes but the underlying address in memory is the same: () { my_num = ; ( , &my_num); my_num = ; ( , &my_num); } fn main let mut 5 println! "my_num pointer address: {:p}" 6 println! "my_num pointer address: {:p}" prints: my_num pointer : x7ffee5d6e6fc my_num pointer : x7ffee5d6e6fc address 0 address 0 There are other interesting keywords to explore as well like and , but we'll save those for another article. const static Thanks For Reading Follow us on Twitter if you have any questions or comments @q_vault Take game-like coding courses on Qvault Classroom to our Newsletter for more educational articles Subscribe Previously published at https://qvault.io/2020/05/13/variable-shadowing-in-rust-let-is-immutable-but-not-constant/