paint-brush
Variable Shadowing: "Let" Keyword Is Immutable But Not Constant Variableby@wagslane
141 reads

Variable Shadowing: "Let" Keyword Is Immutable But Not Constant Variable

by Lane WagnerAugust 19th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Rust is strongly typed and defaults to immutable values. By default new variables are immutable, which means they can't be reassigned. In Rust, "let" declares immutable values, which contributes to Rust being a safer language. We will learn how immutable!= constant by using variable shadowing. In JavaScript, the let keyword is used to declare mutable values, but in Rust, it is allowed to declare a new variable with the same name, even all in the same scope. The name of the variable can easily point to an entirely new variable.

Company Mentioned

Mention Thumbnail
featured image - Variable Shadowing: "Let" Keyword Is Immutable But Not Constant Variable
Lane Wagner HackerNoon profile picture

Let's take a look at some of the common pitfalls with the keywords let and mut. Then, we will learn how immutable != constant by using variable shadowing.

Getting started with Rust can be daunting. Rust is well-known for being a safe language. One of the ways in which Rust is safe is through type-safety. Rust is strongly typed and defaults to immutable values.

The "let" Keyword

The simplest way to create a new variable in Rust is by using the "let" keyword:

fn main() {
    let my_num = 5;
    println!("{}", my_num);
}

let introduces a new variable into the current scope. By default new variables are immutable, which means they can't be reassigned. For example:

fn main() {
    let my_num = 5;
    my_num = 6;
    println!("{}", my_num);
}

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 let keyword allows the compiler to ensure that a given variable can't be changed... kind of. The following does not fail to compile:

fn main() {
    let my_num = 5;
    let my_num = 6;
    println!("{}", my_num);
}

We are allowed to declare a new variable with the same name, even all in the same scope. This doesn't mutate "my_num", it creates a new variable with a new spot in memory. The name "my_num" now refers to the new variable, and the old variable is no longer accessible by its name.

Variable shadowing also works in an inner scope. In the outer scope it is in a way the original variable remains "unshadowed":

fn main() {
    let my_num = 5;
    // start new scope
    {
        let my_num = 6;
        println!("{}", my_num);
    }  
    println!("{}", my_num);
}

prints:

6
5

Notice how the pointer to the new variable is completely different:

fn main() {
    let my_num = 5;
    println!("my_num pointer address: {:p}", &my_num);
    let my_num = 6;
    println!("my_num pointer address: {:p}", &my_num);
}

prints:

my_num pointer address: 0x7ffeee0ad6f4
my_num pointer address: 0x7ffeee0ad74c

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 "mut" keyword:

fn main() {
    let mut my_num = 5;
    my_num = 6;
    println!("{}", my_num);
}

Prints 6

Mutable variables are just that - mutable. The value changes but the underlying address in memory is the same:

fn main() {
    let mut my_num = 5;
    println!("my_num pointer address: {:p}", &my_num);
    my_num = 6;
    println!("my_num pointer address: {:p}", &my_num);
}

prints:

my_num pointer address: 0x7ffee5d6e6fc
my_num pointer address: 0x7ffee5d6e6fc

There are other interesting keywords to explore as well like const and static, but we'll save those for another article.

Thanks For Reading

  • Follow us on Twitter @q_vault if you have any questions or comments
  • Take game-like coding courses on Qvault Classroom
  • Subscribe to our Newsletter for more educational articles

Previously published at https://qvault.io/2020/05/13/variable-shadowing-in-rust-let-is-immutable-but-not-constant/