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

#rust #rustlang #immutable #constant #programming #programming-languages #memory-management #variable-shadowing

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