Menu Close

Rust – Variables in Rust

In this article, we are going to understand Variables and Their Usage in Rust in detail, along with examples.


Rust is a systems programming language designed for safety, performance, and concurrency. Variables are fundamental building blocks in Rust, just like in any other programming language. In this article, we will explore variables in Rust, their characteristics, and how to work with them through examples.

Topics to be covered:

  • Rules for Naming a Variable
  • Declaring Variables in Rust
  • Mutable Variables
  • Immutable Variables
  • Variable Shadowing

Rules for Naming a Variable

In Rust, variable names follow specific rules and conventions to maintain readability, consistency, and clarity in the code. The rules for naming variables in Rust are:

  • Variable names must begin with a Unicode character that is a letter, underscore (_), or a non-decimal digit.
  • After the first character, variable names may contain any combination of Unicode letters, non-decimal digits, and underscores.
  • Variable names are case sensitive, so my_variable and My_Variable are considered different variables.
  • Rust follows the snake_case convention for variable names, where words are separated by underscores and all letters are lowercase.
  • Rust also allows for Unicode characters in variable names, but using them is generally discouraged for readability and compatibility reasons.

Examples of Valid Variable Names:

Here are some examples of valid variable names in Rust:

let my_variable = 5;

let number1 = 42;

let _temp_value = 3.14;

let user_age = 30;

let this_is_a_long_variable_name = "example";

In the examples above, all variable names start with a Unicode letter or an underscore and follow the snake_case convention.

Examples of Invalid Variable Names:

Below are some examples of invalid variable names in Rust:

let 1number = 42; // Starts with a decimal digit

let -my_variable = 5; // Starts with an invalid character

let this is a space = "example"; // Contains spaces

let mixedCaseVariable = 30; // Does not follow the snake_case convention

In these examples, the variable names either start with an invalid character, contain spaces, or do not follow the snake_case convention.

Importance of Naming Conventions:

Following Rust’s variable naming conventions is essential for several reasons:

  • Readability: Consistent naming conventions make code easier to read and understand, improving maintainability.
  • Clarity: Using descriptive variable names helps convey the purpose and meaning of variables, making it easier for others to understand your code.
  • Collaboration: Following established conventions allows for better collaboration with other developers who are familiar with Rust’s naming conventions.

Reserved Keywords:

In Rust, there are certain keywords that are reserved for language constructs and cannot be used as variable names. Some examples of reserved keywords are:

fn, let, const, mut, if, else, while, for, loop, match, break, continue, return, etc.

Declaring Variables in Rust

In Rust, you declare a variable using the let keyword, followed by the variable name, and then the value you want to assign to it. The type can be either explicitly stated or inferred by the compiler based on the value.


fn main() {

    let x = 5; // immutable variable with inferred type (i32)

    let y: i32 = 10; // immutable variable with explicit type (i32)


In the example above, we declared two variables, x and y. The type of x is inferred by the compiler to be i32, while the type of y is explicitly specified as i32.

Mutable Variables

To declare a mutable variable in Rust, you must use the mut keyword after the let keyword. Mutable variables allow you to change their values after assignment.


fn main() {

    let mut x = 5;

    println!("The value of x is: {}", x);

    x = 6;

    println!("The value of x is: {}", x);


In this example, we declared a mutable variable x with an initial value of 5. We then printed its value, changed it to 6, and printed the updated value.

When to Use Mutable Variables:

While mutable variables provide flexibility, they can also introduce potential bugs and make code harder to reason about. As a best practice, you should prefer using immutable variables whenever possible and only use mutable variables when necessary. For example, use mutable variables when dealing with values that need to be updated frequently or when working with data structures that require in-place modification.

Mutable References:

In Rust, you can create mutable references to mutable variables using the &mut keyword. This enables you to modify the value of the variable through the reference.


fn main() {

    let mut x = 5;

    let y = &mut x; // y is a mutable reference to x

    *y += 1; // Dereference y and increment the value

    println!("The value of x is: {}", x);


In this example, we created a mutable reference y to the mutable variable x. We then incremented the value of x through the reference y by dereferencing it with the * operator.

Immutable Variables

Immutable variables are the default in Rust. When you declare a variable using the let keyword, the variable is immutable by default, and its value cannot be changed.


fn main() {

    let x = 5;

    println!("The value of x is: {}", x);

    x = 6; // This will produce a compilation error


In the example above, we declared an immutable variable x with a value of 5. When we tried to change its value to 6, it resulted in a compilation error, as the variable is immutable.

Benefits of Immutable Variables:

Immutable variables offer several advantages in Rust:

  • Predictability: Since the value of an immutable variable cannot change, it is easier to reason about the program’s behavior.
  • Safety: Immutability prevents unwanted side effects and ensures that variables’ values are not accidentally altered.
  • Concurrency: Immutable variables do not require locks or other synchronization primitives, making it easier to write concurrent and parallel code.

When to Use Immutable Variables:

As a best practice, you should always use immutable variables in Rust unless you need to change a variable’s value after assignment. Immutable variables help maintain predictability, safety, and performance in your Rust code.

Variable Shadowing

When you declare a variable using the let keyword, the variable is immutable by default, and its value cannot be changed. However, Rust provides a feature called “shadowing” that allows you to declare a new variable with the same name as an existing one. This new variable “shadows” the original variable, effectively creating a new scope for it.


fn main() {

    let x = 5;

    let x = x + 1;

    let x = x * 2;

    println!("The value of x is: {}", x);


In this example, we declared an immutable variable x with a value of 5. We then shadowed it twice, first by adding 1 and then by multiplying by 2. The final value of x is 12.

Benefits of Shadowing:

Shadowing offers several advantages in Rust:

  • Safety: Shadowing allows you to change the value of an immutable variable while maintaining its immutability, preventing unwanted side effects and ensuring that the variable’s value is not accidentally altered.
  • Flexibility: Shadowing enables you to reuse variable names, which can be helpful in scenarios where you want to perform multiple transformations on a value.
  • Type Transformation: Shadowing can also be used to change a variable’s type, allowing for more flexible programming.

To check more Go related articles. Pls click given below link:

Posted in Rust

Leave a Reply

Your email address will not be published. Required fields are marked *