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
// 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
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
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
mutto 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
1. What is the default mutability of a variable declared with `let` in Rust?
2. Which keyword allows a variable's value to be changed after declaration?
3. How many bytes does Rust's `char` type occupy?
4. Which of these is a compound type in Rust?
5. What happens if you try to reassign a value to an immutable variable in Rust?
Was this page helpful?
You May Also Like
Constants and Shadowing in Rust
Understand Rust constants declared with const and the concept of variable shadowing with let.
Operators in Rust
Explore Rust's arithmetic, comparison, logical, and bitwise operators with practical examples.
Type Conversion in Rust
Learn how Rust handles explicit type conversion using the as keyword and the From/Into traits.
Tuples and Arrays in Rust
Tuples group a fixed set of differently-typed values together, while arrays hold a fixed-size list of same-typed values.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics