Spoiler Alert I am not a good content writer....
I am a JS developer and want to learn some cool language. I came across Rust. Now I am learning it. The best way to learn something is to teach others.
Let's start from the beginning. I mean to say let's talk about variable
in Rust.
The most important thing about Rust, Rust is a tightly typed Language. I will talk about types in Rust in the future currently I will only talk about variables in Rust.
In Rust, we follow snake_case
for naming a variable. Here is the example of snake_case
. By the way, snake_case
is a self-example of snake_case
. In snake_case
add _
.
// variable names example in snake_case
is_value_true
sum_all_marks
we use the let
keyword for declaring a variable.
let snake_case = 10; // semi colon is very important
That is all about variables. The End...
Just Kidding.
Variables are immutable
in Rust. In simple words, Immutable means not changeable.
Note: Rust is memory efficient it will not allow you to create unused variables.
Take an example from this code.
let snake_case = 10;
snake_case = 20; // it will throw error
println!("Here is value {snake_case}");
Mutable Variables
We will get an error because you can not change the value of snake_case
. If you want to change the value then you need to create a mutable variable
.
Now everything is working according to expectations. Pretty cool.
There is another way to change the value without using the mut keyword
. As I told Rust is memory efficient. Therefore you need to re-declare the variable. let
let snake_case = 10;
let snake_case = 20;
println!("Here is value {snake_case}"); // Here is value 20
There is one difference between these two. As I told earlier, Rust is a tightly typed Language. When we declare a variable with mut keyword
it will assign the type according to the value and when we change the value we will not be able to change the type.
let mut snake_case = 10; // type => i32
snake_case = "20"; // throw error because we cannot change variable type
println!("Here is value {snake_case}");
On other hand
let snake_case = 10; // type => i32
let snake_case = "20"; // Now type is changed from i32 to string
println!("Here is value {snake_case}");
This is all about Variables in Typescript. I hope you enjoyed and don't forget to give feedback. See you !!