Part-3: Variables and Mutability in Rust
Tags: rust, programming, tutorial, learning
Published: 2024-01-25 14:25:10

Hey folks, welcome to part-3 of my blog series on rust. In this series we are learning rust programming language from scratch, if you haven't read the previous parts then i highly recommend to read those first so that you get the context of what we are doing here. Read [here](https://dev.to/codezera/series/26110).

So without wasting any time lets get started!

---

# Mutability

To understand mutability better, lets try to run a simple code. Paste the following code on your VS code or whatever IDE you use.

```
fn main() {
    let x = 1;
    println!("The value of x is: {x}");
    x = 2;
    println!("The value of x is: {x}");
}
```

What this code does is just assign x variable a value of 5 and then print it. After that we try to change the value of x to 6 and again print it.

Now if this would have been a language like javascript then where would have been no issues with this code but my friends with rust life is not that easy. Go ahead and try to compile this code (SPOILER: You'll get an error!).

You'll get get error message something like "cannot assign twice to immutable variable `x`".

This happened because by default all variables in rust are immutable, meaning you cannot change the value of a variable if you've declared it, but it does not mean you cannot change it by anyway. There is a way, try running the following code:

```
let mut x = 1;
println!("The value of x is {x}");

x = 2
println!("The new value of x is {x}");
```

Now the above code will work perfectly fine. Meaning you just have to specify while declaring the variable that it is mutable or not, by default all variables are immutable but you can change this behaviour by writing `mut` keyword just before the variables name.

> When we code in rust compilers errors are actually good, it doesn't mean you're a bad developer, even the experts get compile time errors because rust tries to eliminate all possible bugs beforehand.

---

# Constants

Constants are declared like following:

```
const x = 5;
```

Constants are just like immutable variables i.e. You are not allowed to change them but there are some differences.

First, You cannot use `mut` keyword with constants. Constants are always immutable, you cannot change this behaviour at all.

Second, Constants can be declared in any scope. Which is very useful especially when you want something like global variables.

Third, Constants can be set only to a constant expression, which basically means that you cannot set constant variables to a result which will be calculated at runtime.

> Rust’s naming convention for constants is to use all uppercase with underscores between words.


---

# Shadowing

In rust you can shadow variables meaning you can declare a new variable with the same name as the previous variable. This case is generally called `Shadowing`. An example of shadowing is mentioned below:

```
let x = 1;
let x = x + 1;

println!("The value of x is {x}");
```

We can say that first variable is shadowed by second, which basically means that compiler will see the second variable at the compile time.


## Is shadowing same as mut?

You might be thinking shadowing is same as mut, but these both are actually different, because we'll get a compile time error if we accidentally try to reassign value to variable `x` without using the `let` keyword. By using let, we can perform transformations to the variable but it will be immutable after those transformations are done.

There is one more difference between `mut` and shadowing, you can also change the type of the variable when you do shadowing. You can understand this with the help of following example:

```
let x = "    ";
let x = x.len();
```

The first variable x is a string type(spaces are completely valid in rust and are treated as string) and second is number type. Basically you can change the type if you do shadowing. But if we try to do mutation here like following:

```
let x = "    ";
x = x.len();
```

We'll get compile-time error.

---

# That's It!

Today we learned the concept of mutability in rust, also what are constants and the concept of shadowing in rust and how it is different from mut.

Thats it for this blog, see you guys in the next one. Till then!

Happy Coding!

---

You can find me on twitter [here](https://twitter.com/yadav_bhavesh73).

---