Rust Variables and Mutability

Rust Programming

Rust Programming

In computer programming, we use variables to store data. For example,

let x = 1;{codeBox}

Here, x is the name of the variable which stores the value 1.

We can think of variables as containers that hold information.

Rust Variable Declaration

We use the let keyword to declare a variable in Rust.

let age = 31;{codeBox}

Here, we have created a variable named age with value 31.

Example: Rust Variables

fn main() {
    // variable to store integer value
    let age = 31;
    println!("Age: {}", age);

    // variable to store floating-point value
    let salary = 342523.23;
    println!("Salary: {}", salary);

    // variable to store string
    let name = "Jackie";
    println!("Name: {}", name);


Age: 31
Salary: 342523.23
Name: Jackie{codeBox}

In the above example, we have created three different variables:

  • age - to store an integer value
  • salary - to store a floating-point data
  • name - to store a string

Notice that we have used the println! macro to print the variables.

By default, Rust variables are immutable, which means we cannot change the value of a variable once it is defined. If we try to change the value, It will give error. In order to change the variable we use Mutability in Rust. Here's how can we change value:

Mutability in Rust

We use the mut keyword before the variable name to create a mutable variable. For example,

let mut x = 1;{codeBox}

Here, x is a mutable variable. Now we can change the value of x.

Example: Mutable Variables

fn main() {
    // declare a mutable variable with value 1
    let mut x = 1;
    println!("Value of x = {}", x);

    // change the value of variable x
    x = 2;
    println!("Updated value of x = {}", x);


Value of x = 1
Updated value of x = 2{codeBox}

Here, you can see we have successfully changed the value of the x variable. This is because we have defined the x variable using the mut keyword.

Rules for Naming Variables in Rust

We can use any names as variable names, however, there are some rules we should follow:

1. Rust is a case sensitive language. Hence, lowercase variables and uppercase variables are different. For example,

  • age is different from AGE
  • name is different from Name

2. Variables must start with either a letter or an underscore. For example

  • let age = 31; // valid and good practice
  • let _age = 31; // valid variable 
  • let 1age = 31; // inavlid variable

3. Variable names can only contain letters, digits and an underscore character. For example,

  • let age1 = 31; // valid variable
  • let age_num = 31; // valid variable
  • let s@lary = 52352; // invalid variable

4. Use underscore if we need to use two words as variable names. For example,

  • let first name = "Jack"; // invalid variable
  • let first_name = "Jack"; // valid variable
  • let first-name = "Jack"; // invalid variable

Rust Constants

A constant is a special type of variable whose value cannot be changed. We use the const keyword to create constants in Rust. For example,

fn main() {
    // declare a float constant
    const PI: f32 = 3.14;
    println!("Value of PI = {}", PI);


Value of PI = 3.14{codeBox}

In the above example, we have declared a constant PI with value 3.14. Now, the value of PI cannot be changed throughout the program.

Credits & Copyrights©: Javatpoints & programiz

Post a Comment