In this tutorial you will learn about the Rust Variables and its application with practical example.
What is Variables?
Variables is an identifier used to refer memory location in computer memory that holds a value for that variable, this value can be changed during the execution of the program. When you create a variable in Rust, this means you are allocating some space in the memory for that variable. The size of memory block allocated and type of the value it holds is completely dependent upon the type of variable.
Declaring Variables In Rust
In Rust, we can declare variable bindings first, and initialize them later. In Rust, variables are declared using the let keyword followed by variable name that you want to declare, optionally a colon (:) and then the data type you want to hold in that variable.
Syntax:-
1 |
let <name>; |
or
1 |
let <name> : <type>; |
Initializing Variables In Rust
In Rust, a variable bindings must be initialized with a value before they are used. The assignment operator (=) is used to assign values to a variable, the operand in the left side of the assignment operator (=) indicates the name of the variable and the operand in the right side of the assignment operator (=) indicates the value to be stored in that variable.
Syntax:-
1 |
let <name> = <value> |
or
1 |
let <name>:<type> = <value> |
or
1 2 |
let <name>:<type>; <name> = <value>; |
or
1 2 |
let <name>; <name> = <value>; |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fn main() { let a; // Declaration without type let b : i32; // Declaration with type let c: i32 = 10; // Declaration with initialization with type let d = 20; // Declaration with initialization with type inference a = 10; b = 20; println!("W3Adda Variable Initialization"); println!("a: {}", a); println!("b: {}", b); println!("c: {}", c); println!("d: {}", d); } |
Output:-
Type Annotations
Rust is a type inferred language, which allows us to drop the type annotation from the declaration. In Rust, compiler automatically infer(know) the type of data we want to store based on the initial value we assign. In Rust, this feature is termed as ‘type inference’.
Example:-
1 |
let myvar = 10; |
Here, type annotation is not used in the declaration but the Rust compiler simply infer the type automatically for us.
Rust Mutability
In Rust, by default variables are immutable. Once a variable is assigned an initial value, you will not allowed to change that value.
Example:-
1 2 |
let a = 5; a = 10; |
This will result in error –
1 2 3 |
error: re-assignment of immutable variable `a` a = 10; ^~~~~~~ |
However, if you still have the option to make your variables mutable by adding the mut keyword in front of variable name.
Example:-
1 2 |
let mut a = 5; a = 10; |
Printing Variables or String Interpolation
In Rust, println!
() function is used to print the current value of a variable. String interpolation can be done by including two curly braces ({}
) in your string to print, Rust compiler will interpret this as a request to interpolate with some value. Next, the comma is used to separate arguments we are interpolating with.
Example:-
1 2 3 4 5 |
fn main() { let a = 50; println!("W3Adda String Interpolation"); println!("The value of a is: {}", a); } |
Output:-