Rust
December 2, 2026 6 min read

Variables and Data Types

HS
HEMANT SINGH
Technical Writer & Educator

Lesson 3: Variables and Data Types in Rust

1. What is a Variable?

A variable is a named storage location used to store data in a program.

For example, we can store a name, age, price or quantity inside a variable.

Example

fn main() {
let name = "Hemant";
let age = 32;

println!("Name: {}", name);
println!("Age: {}", age);
}

Output:

Name: Hemant
Age: 32

Explanation:

  1. let is used to declare a variable.
  2. name is the variable name.
  3. "Hemant" is the stored value.
  4. age is another variable.
  5. 32 is its stored value.

2. The let Keyword

In Rust, the let keyword is used to declare a variable.

Syntax

let variable_name = value;

Example

fn main() {
let name = "Hemant";
let age = 32;
let price = 450.50;
let is_active = true;
}

3. Immutable Variables

By default, Rust variables are immutable.

Immutable means their values cannot be changed after initialization.

Example

fn main() {
let age = 32;

println!("Age: {}", age);

// age = 33; // This will cause a compilation error
}

Important: Rust makes variables immutable by default to prevent accidental changes to data.

4. Mutable Variables

If you want to change a variable's value, use the mut keyword.

Syntax

let mut variable_name = value;

Example

fn main() {
let mut age = 32;

println!("Before: {}", age);

age = 33;

println!("After: {}", age);
}

Output:

Before: 32
After: 33

Immutable vs Mutable


Immutable

Mutable

Cannot change its value

Can change its value

Uses let

Uses let mut

Default behaviour

Requires explicit permission

Example: let x = 10;

Example: let mut x = 10;

5. Constants

A constant is a value that cannot be changed throughout the program.

Rust uses the const keyword to declare constants.

Syntax

const CONSTANT_NAME: DATA_TYPE = value;

Example

const PI: f64 = 3.14159;

fn main() {
println!("PI: {}", PI);
}

Output:

PI: 3.14159

Rules of Constants

  1. Constants must have an explicit data type.
  2. Constants cannot be declared using mut.
  3. Constants must be initialized when declared.
  4. Constant values must be known at compile time.

Variable vs Constant


Variable

Constant

Declared using let

Declared using const

Can be mutable using mut

Always immutable

Type can be inferred

Type must be specified

Can be initialized at runtime

Value must be a compile-time constant

6. Shadowing

Shadowing means declaring a new variable with the same name as an existing variable.

Rust allows this using let.

Example

fn main() {
let age = 25;

let age = 30;

println!("Age: {}", age);
}

Output:

Age: 30

The second declaration creates a new variable named age, which shadows the first one.

Shadowing with Different Data Types

Rust also allows shadowing with a different data type.

fn main() {
let number = "100";

let number = 100;

println!("Number: {}", number);
}

Output:

Number: 100

Here:

  1. First number is a string.
  2. Second number is an integer.

Shadowing vs Mutability


Shadowing

Mutability

Uses let again

Uses let mut

Creates a new variable

Changes the existing variable

Can change data type

Cannot change the variable's declared type

Example: let x = 20;

Example: x = 20;

7. Data Types in Rust

Rust is a statically typed programming language.

This means every variable has a data type known at compile time.

Rust has two major categories of data types:

  1. Scalar Types
  2. Compound Types

A. Scalar Data Types

Scalar types represent a single value.

Rust has four primary scalar types:

  1. Integer
  2. Floating-point
  3. Boolean
  4. Character

8. Integer Data Types

Integer types represent whole numbers without decimal points.

Examples:

10
100
-50
5000

Rust provides signed and unsigned integer types.

Signed Integer Types

Signed integers can store both positive and negative values.


Type

Size

i8

8 bits

i16

16 bits

i32

32 bits

i64

64 bits

i128

128 bits

isize

Architecture dependent

Unsigned Integer Types

Unsigned integers can store zero and positive values only.


Type

Size

u8

8 bits

u16

16 bits

u32

32 bits

u64

64 bits

u128

128 bits

usize

Architecture dependent

Example

fn main() {
let age: i32 = 32;
let quantity: u32 = 100;
let temperature: i8 = -10;

println!("Age: {}", age);
println!("Quantity: {}", quantity);
println!("Temperature: {}", temperature);
}

Output:

Age: 32
Quantity: 100
Temperature: -10

9. Floating-Point Data Types

Floating-point types represent numbers with decimal points.

Rust provides two floating-point types:


Type

Size

Precision

f32

32 bits

Single precision

f64

64 bits

Double precision

Rust uses f64 as the default floating-point type.

Example

fn main() {
let price: f64 = 450.50;
let discount: f32 = 10.5;

println!("Price: {}", price);
println!("Discount: {}", discount);
}

Output:

Price: 450.5
Discount: 10.5

10. Boolean Data Type

Boolean represents only two values:

  1. true
  2. false

Its data type is bool.

Example

fn main() {
let is_active: bool = true;
let is_logged_in: bool = false;

println!("Active: {}", is_active);
println!("Logged in: {}", is_logged_in);
}

Output:

Active: true
Logged in: false

11. Character Data Type

The char type represents a single Unicode scalar value.

It uses single quotation marks.

Example

fn main() {
let letter: char = 'A';
let symbol: char = '@';
let emoji: char = '😊';

println!("Letter: {}", letter);
println!("Symbol: {}", symbol);
println!("Emoji: {}", emoji);
}

Output:

Letter: A
Symbol: @
Emoji: 😊

Important: A Rust char represents one Unicode scalar value, not necessarily one byte.

12. Type Inference

Rust can automatically determine a variable's data type from its assigned value.

This is called type inference.

Example

fn main() {
let age = 32;
let price = 450.50;
let is_active = true;

println!("Age: {}", age);
println!("Price: {}", price);
println!("Active: {}", is_active);
}

Rust automatically infers:


Variable

Inferred Type

age

i32

price

f64

is_active

bool

These are the default inferred types in this example.

13. Type Annotations

You can explicitly specify a variable's data type.

Example

fn main() {
let age: i32 = 32;
let price: f64 = 450.50;
let is_active: bool = true;

println!("Age: {}", age);
println!("Price: {}", price);
println!("Active: {}", is_active);
}

Type Inference vs Type Annotation


Type Inference

Type Annotation

Rust determines the type

Programmer specifies the type

Less code

More explicit

let age = 32;

let age: i32 = 32;

14. Type Conversion and Casting

Sometimes we need to convert one data type into another.

Rust supports explicit numeric casting using the as keyword.

Example: Integer to Float

fn main() {
let number: i32 = 100;

let decimal: f64 = number as f64;

println!("Integer: {}", number);
println!("Float: {}", decimal);
}

Output:

Integer: 100
Float: 100

Example: Float to Integer

fn main() {
let price: f64 = 99.99;

let number: i32 = price as i32;

println!("Price: {}", price);
println!("Integer: {}", number);
}

Output:

Price: 99.99
Integer: 99

Important: Converting a floating-point number to an integer using as discards the fractional part.

15. Important Summary


Concept

Syntax

Purpose

Variable

let x = 10;

Declare a variable

Mutable Variable

let mut x = 10;

Declare a changeable variable

Constant

const PI: f64 = 3.14;

Declare a constant

Shadowing

let x = 20;

Create a new variable with the same name

Integer

let x: i32 = 10;

Store whole numbers

Float

let x: f64 = 10.5;

Store decimal numbers

Boolean

let x: bool = true;

Store true or false

Character

let x: char = 'A';

Store one Unicode scalar value

Casting

x as f64

Convert numeric types

16. Practice Exercises

Exercise 1: Basic Variables

Create variables for:

  1. Your name
  2. Your age
  3. Your city
  4. Your company name

Print all values.

Exercise 2: Mutable Variables

Create a mutable variable named salary.

  1. Assign 10000.
  2. Print the salary.
  3. Change it to 15000.
  4. Print the updated salary.

Exercise 3: Constants

Create a constant named GST_RATE with a value of 18.0.

Print the GST rate.

Exercise 4: Data Types

Create the following variables with explicit data types:

  1. age: i32
  2. price: f64
  3. quantity: u32
  4. is_available: bool
  5. grade: char

Print all values.

Exercise 5: Shadowing

Create a variable named number with the string value "500".

Then shadow it with an integer value of 500.

Print the final value.

Exercise 6: Type Casting

Create an integer variable with value 25.

Convert it into a floating-point value and print both variables.