Variables and Types in Rust
This is the fourth part of my “rust” series. For learning rust from scratch consider reading these three articles first.
Rust mentioned here is not about the degradation of iron with water and air. In this complete article, we will try to…medium.com
Like every new language, you learn the first thing you encounter is the word “Hello, World!”. It is just the tradition…medium.com
In my previous stories, we discussed RUST!! installation, we also ran our first “Hello, World!” program.medium.com
In the last article, we learned about formatting in “rust”.Today we will learn about variables and types in Rust. Every programming language has a concept of variables.
What is a variable?
According to Wikipedia,
A variable is a storage location (identified by a memory address) paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context.
In simple words, if we have some value lets assume my name “Rizwan”, this is a string and we want to store it in our storage device. We need a variable to store our value, as it gives us an identifier for every data we store so as it is easy to use anywhere in our program.
In Rust, we can define a variable using the “let” keyword.
Variables are immutable by default in rust.
Why they are made immutable by default ?? you will get the answer soon
How it is different from other variables ??
suppose we define a variable num,
let _num = 40; // immutable by default
num = 45; // this will give us an error
We have defined two variables name and age now try to change the value of age.
Rust compiler throws an error and also shows the correct way to make a variable immutable, yes with the use of mut keyword.
We can make the variable mutable like above illustrated in the picture. It is showing a little warning as we are not using the previous value. Let’s get rid of this error.
We also have a const keyword and also if we are defining const we have to explicitly mention its type.
const ID:i32 = 5; ///////type will be discussed later////////
We can also define multiple variables at the same time and assign the value to them.
Types in Rust
let name = “John Doe” ; // type is string
let age = 40; // type is number
I am not mentioning the type here, so ?????????
No, not really “Rust is a statically typed language” but for ease of the programmers the creators of “Rust” made Type defining not compulsory, but don't think it like dynamically typed because at compile time rust compiler knows about each type of variable defined and interpret it according to our use case.
Of course, we can explicitly define the type of variables like,
let _age : i32 = 23; // type is integer 32 bit
let _value : f64 = 67.77; // type is float 64 bit
Primitive types in Rust
Integer: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128 (8,16,32,64 and 128 bits);
Float: f32, f64;
Boolean: (bool) true, false ;
By default integer type is i32 and float is of f64
We can find the max of each type so as we know where to use which type.
In the above println! statement we are calling the std(std library) then type and then MAX;
Boolean will give us true and false as a result.
Both implicit and explicit definition of a boolean type.
Other types will be discussed in the next article stay tuned and keep Rusling!!!!
Feel free to ask any questions!!!!!!!!