DEV Community

Cover image for Common Programming Concepts - Part 1 (Variables and Mutability)
Aditya Verma
Aditya Verma

Posted on • Originally published at aditya-22.hashnode.dev

Common Programming Concepts - Part 1 (Variables and Mutability)

What are we covering in this article?

As we established in the last article, “we are all nerds”. So this section might just be a revision for your basic programming concepts, and if you don’t know some then this is a great learning opportunity. This will also lay the foundation for all upcoming articles and all Rust specific concepts as well, so we need to make sure that these are very clear to us.

We will be covering the basics like Variables and Mutability, Data Types, Functions, Comments and Control Flow. These topics are large so we will split this topic into 4 parts that will be divided into 4 articles.

Variables and Mutability

Variables in simple language are just containers that stores a type of value. When we need a value at any step we get that value using the variable name in which we stored the value. For example, if we need to store the number 22, we will create a variable with a name and assign that value to it. So in this case let’s say I choose the name num, I will assign num = 22. This way whenever I need the value 22, I will use num.

Now that we are clear in what a variable is let’s talk about mutability. Mutability is a very important concept in Rust as it adds an important layer to the whole security and control aspect of Rust. Mutability in plain english means the ability to change. Meaning that if something can change then it is mutable and if it cannot then it is immutable. In Rust all variables are immutable by default meaning once we assign a value to them we cannot change that value.

Now that we are clear with both the concepts let’s see how we can create and use variables in Rust. To create a variable we use the keyword let to tell Rust that we will be creating a variable. It is then followed by the name of the variable which in our example is num, so it becomes
let num . Then we write = to assign it a value followed by the value, let num = 22 . We have just declared a variable called num and assigned it the value 22. Now we know that by default variables are immutable so if we ever want to change the value of num we cannot. To change this we are given a keyword called mut . This keyword when used while creating a variable makes it mutable. So if we added it to our declaration from before like let mut num = 22 , then we would be able to change the value of num whenever we want.

Seeing it in action

That’s a lot of talking, let’s see it in action in code. We will also see how the error looks for if we try to change the variable without using mut . I created a new cargo project for this section. So we start by doing cargo new programming-basics . Then we go to /programming-basics/src/main.rs and we code there.

Here first we simply try to declare a variable and print it.

fn main() {
    let num = 22;
    println!("{num}");
}
Enter fullscreen mode Exit fullscreen mode

Output will be:

❯ cargo run
   Compiling programming-basics v0.1.0 ({path}/programming-basics)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/programming-basics`
22
Enter fullscreen mode Exit fullscreen mode

Checking the error

Now we try to mutate the variable.

fn main() {
    let num = 22;
    println!("{num}");
    num = 21;
    println!("{num}");

}
Enter fullscreen mode Exit fullscreen mode

When we try to cargo run this we get the following output in the terminal:

❯ cargo run
   Compiling programming-basics v0.1.0 ({path}/programming-basics)
error[E0384]: cannot assign twice to immutable variable `num`
 --> src/main.rs:4:5
  |
2 |     let num = 22;
  |         --- first assignment to `num`
3 |     println!("{num}");
4 |     num = 21;
  |     ^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut num = 22;
  |         +++

For more information about this error, try `rustc --explain E0384`.
error: could not compile `programming-basics` (bin "programming-basics") due to 1 previous error
Enter fullscreen mode Exit fullscreen mode

This error clearly states that we ‘cannot assign twice to immutable variable num’.

Making it mutable

Now we try making the variable mutable and running the same program again.

fn main() {
    let mut num = 22;
    println!("{num}");
    num = 21;
    println!("{num}");
}
Enter fullscreen mode Exit fullscreen mode

The output is :

❯ cargo run
   Compiling programming-basics v0.1.0 ({path}/programming-basics)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/programming-basics`
22
21
Enter fullscreen mode Exit fullscreen mode

As we can clearly see the reassignment of value worked without any error. We first print a 22 but after changing the value when we print we get a 21. This is the mut keyword playing its part perfectly!

Conclusion

Let’s end it here for now. We learnt what a variable is and what is mutability. Then we learnt about the mutability of variables in Rust and how can we implement them in code. We also learnt how we can change the mutability of a variable. Next article we learn about data types, see you all then!

Top comments (0)