Variables are used to store the type data in a memory location.

How to declare a variable in Rust?

Here is a syntax for variable declaration in Rust.

let variable=value // type annotation without type
let variable:datatype=value // with type declaration

the variable is the name of a variable datatype is an valid Rust DataType value is any valid literal in Rust.

In the Next section, Let’s see the rules and conventions for naming a variable.

Here is an example

fn main() {
    let name = "John";
    let id: u8 = 20;
    println!("{} - {}", id, name)

Rust variable naming conventions

Following are naming conventions for variable names

  • Variable Name is a combination of letters, digits, and underscore(_) character

  • Variable names start with letters and digits only, numbers are not allowed

  • keywords and identifiers in Rust are not used as a variable name

  • Local variables follow snake_case conventions, for example, employee_name.

  • Variable names are case sensitive, which means upper case and lower case names are different.

In the first line, the data type is not declared, the compiler is inferred from the value.

immutable variables

Variables in Rust are immutable by default, which means, that once the variable is assigned with value, Its value will never be changed.

fn main() {
    let number = 2;
    number = 20;
    println!("{}", number)

It throws a compilation error[E0384]: cannot assign twice to immutable variable `number``

How to declare mutable variables

Variables declared with the mut keyword are mutable variables.

These variables can be changed its value multiple times, reassign with new values

fn main() {
    let mut number = 2;
    number = 20;
    println!("{}", number)

How to declare a variable without value?

It is possible to declare a variable first and assign the value later.

This variable is declared with type, but not initialized with value in the declaration. Next line, assign the variable with a value.

fn main() {
    let age: u8;
    age = 123;
    println!("{}", age);

Rust Variable Scoping

scope of a variable is how the variable is visible and accessible in a code.

Rust follows lexical scoping in which a variable is accessible inside a block on which it is declared. There are two types of scopes

  • Global scopes: variables declared in the main function are called global scopes
  • Local scopes: variables declared inside a function or block have a local scope.

We have inner blocks and outer blocks on which scope is bound to {}.

Variables declared in the outer scope are accessible to the inner scope. But inner scope variables are not accessible in the outer scope

fn main() {
    let age = 25;
        let id = 2;
        println!("age - {} | id - {}", age, id);

Rust variable shadowing

Shadowing makes variables change their value and type.

One variable is declared and assigned with a type, You can reassign it with a new type, and its value In the below example, the variable name is declared of type &str. Reassigned this variable with a new type of value. Here is a valid code

fn main() {
   let name = "John";
    println!("{}", name);

    let name=123;
    println!("{}", name);


How to declare multiple variables of different types?

We can declare multiple variables of the same or different types.

multiple variables are declared with comma-separated enclosed in (). values also placed by comma separator enclosed in ().

In this example, multiple variables are declared with bool, number, and string and initialized with different values in the declaration.

Here is an example program

fn main() {
    let (first, second, third) = (false, 25, "John");
    println!("first: {} | second : {} | third = {}", first, second, third);


first: false | second : 25 | third = John