100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Variables and Data Types in Rust

Learn how Rust variables are immutable by default and explore its scalar and compound data types.

BasicsBeginner8 min readJul 8, 2026
Analogies

Introduction

In Rust, variables are declared with the let keyword. Unlike most languages, Rust variables are immutable by default, meaning once a value is bound to a name, it cannot be changed unless you explicitly mark it as mutable with mut. This design choice helps prevent accidental state changes and makes concurrent code easier to reason about. Rust is also statically and strongly typed, so every variable has a data type known at compile time, either inferred by the compiler or explicitly annotated.

🏏

Cricket analogy: Once a bowler's field placement is set for the over with let, it's locked immutable by default like Rust's bindings — the captain must explicitly signal mut to rearrange fielders mid-over, preventing accidental chaos in the field.

Syntax

rust
// Immutable variable (default)
let x = 5;

// Mutable variable
let mut y = 10;
y = 15; // allowed because y is mutable

// Explicit type annotation
let z: i64 = 100;

// Compound types
let tup: (i32, f64, char) = (500, 6.4, 'r');
let arr: [i32; 3] = [1, 2, 3];

Explanation

The let x = 5; statement creates an immutable binding, so trying to reassign x later would cause a compile-time error. Adding mut before the variable name, as in let mut y = 10;, tells the compiler this binding may change. Rust's scalar types cover integers (i8 through i128, isize, and their unsigned u-prefixed counterparts), floating-point numbers (f32 and f64), booleans (bool), and characters (char, which represents a 4-byte Unicode scalar value, not just ASCII). Compound types like tuples group values of different types together, while arrays hold a fixed number of elements of the same type.

🏏

Cricket analogy: let x = 5; is like the umpire finalizing the over count as 5 — you can't reassign it later; adding mut as in let mut y = 10; is like keeping a live run tally that updates each ball, and Rust's scalar types (i8 to i128, f32/f64, bool, char) are like different scoring formats from wickets to boundary counts to a single Unicode team emoji.

Example

rust
fn main() {
    let mut count = 0;
    count += 1;

    let name: char = 'R';
    let pi: f64 = 3.14159;
    let is_active: bool = true;

    let coordinates: (i32, i32) = (10, 20);
    let numbers: [i32; 4] = [1, 2, 3, 4];

    println!("count = {}", count);
    println!("name = {}, pi = {}, active = {}", name, pi, is_active);
    println!("coordinates = {:?}", coordinates);
    println!("numbers = {:?}", numbers);
}

Output

text
count = 1
name = R, pi = 3.14159, active = true
coordinates = (10, 20)
numbers = [1, 2, 3, 4]

Key Takeaways

  • Variables are immutable by default; use mut to allow reassignment.
  • Rust infers types automatically but supports explicit annotations.
  • Scalar types include integers, floats, booleans, and char (4-byte Unicode).
  • Compound types include tuples (mixed types) and arrays (fixed size, same type).
  • Immutability by default is a core Rust design choice for safety and predictability.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#VariablesAndDataTypesInRust#Variables#Data#Types#Syntax#StudyNotes#SkillVeris