Rust Variables and Mutability

Variables

The variables say to memory locations in a system that holds worth for that variable. These values are mostly changed during the execution of the program.
The variables are only valid within the scope during which they’re declared.

Types of Variable

Mutable Variables:

Immutable Variables:

  • Variables whose value can’t be modified.

The variables are immutable in Rust by default for safety. However we will change the mutability of variables

Example: Changing immutable variable

fn main() {

let x = 5;

println!("The value of x is:

{}", x);

x = 6;

println!("The value of x is:

{}", x);

}
Result

This program would give a compile-time error because we try to vary the worth of an immutable variable.
Example: Changing mutable variable

fn main() {

let mut x = 5;

println!("The value of x is:

{}", x);

x = 6;

println!("The value of x is:

{}", x);

}
Result:

The value of x is 5

The value of x is 6

Constants

The Constants are the values which are sure to a reputation and typically hold those values which we don’t want to vary.
The Constants are declared by using the “const” keyword and therefore the sort of the worth must be annotated. Syntax: const MAX_POINTS: u32 = 100_000;
For constants in Rust’s naming convention is to use all uppercase with underscores between words.
The Constants are only used as hard-coded values which you would like to use throughout your program.
The Constants are mostly declared in any scope, including the worldwide scope, which makes them useful for values that a lot of parts of the code got to realize.

Example: Speed of sunshine, pi(π)

Shadowing

The Shadowing is that the explanation of a replacement variable with an equivalent name because of the previous variable, and therefore the new variable shadows the previous variable.
A variable is often shadowed with a replacement type. For E.g. String are often shadowed as an integer.
we would shadow a variable by using an equivalent variable’s name and repeating the utilization of the “let” keyword.

Example: Shadowing Variable

fn main() {

let x = 5;

let x = x + 1;

let x = x * 2;

println!("The value of x

is: {}", x);

}

Result:

The value of x is 12

Note: Variable remains immutable.

Difference between Shadowing and Mutability

The difference between mut and shadowing is due to that we’re effectively creating a replacement variable when
We use the let keyword again; we will change the sort of the worth but reuse an equivalent name.
we’ll receive a compile-time error if we accidentally attempt to reassign value to an immutable variable without using the let keyword.
We will change in shadowing the sort of the worth but reuse an equivalent name.

Example: Changing data type by shadowing

let spaces = " ";

let spaces = spaces.len();

Note:  this may  change  the info  type from string to integer.

Example

let mut spaces = " ";

spaces = spaces.len();

Note: By using ‘mut’ we aren't allowed to vary the info sort of the variable.