Variables & Types
You declare a variable in Rust with the let keyword, and by default it is immutable: you cannot assign it a new value unless the declaration says mut. This is not pedantry but a way to make code predictable: when there is no mut, the reader knows for sure the value will not change. Next to it lives shadowing — a repeated let with the same name creates a new variable and can even change the type. For values known at compile time there are const and static, and a mutable global static mut is reachable only through unsafe.
Rust's types are strict. Integers differ in sign and width (i8…i128, u8…u128, isize and usize), the default integer is i32 and the default float is f64, and a char takes 4 bytes and holds any Unicode character. There are no implicit conversions between numbers: even i32 to i64 is converted explicitly — with as, From or TryFrom. Integer overflow crashes a debug build with a panic, while in a release build the number silently wraps around, which is why important arithmetic has checked_*, wrapping_* and saturating_*. Simple types are combined into tuples and [T; N] arrays, and every index into an array is checked. This topic walks through it all layer by layer — from let to arrays.
Topic map
- Variables and Mutability —
letis immutable by default, reassignment is errorE0384;let mutallows changing the value; withoutmutyou cannot change the contents either, not just the name. - Shadowing — a repeated
letcreates a new variable with the same name; shadowing can change the type andmutcannot; shadowing inside a block ends with the block. - const and static — a
constis substituted at every place it is used, astaticoccupies one place in memory; both require a type and a value computable at compile time;static mutonly throughunsafe. - Scalar Types — integers
i8…i128andu8…u128,isizeandusizefor indexes and sizes;i32andf64by default;boolis not a number;charis 4 bytes, one Unicode scalar value. - Integer Overflow — a panic in a debug build, wrap-around in a release build;
checked_*,wrapping_*,saturating_*andoverflowing_*make the behaviour explicit. - Type Conversion — there are no implicit conversions;
ascan truncate a number and will not tell you;FromandIntoare lossless,TryFromis checked,parsereads from a string. - Tuples and Arrays — a tuple groups values of different types,
()is the empty tuple; an array[T; N]holds N values of one type, the length is part of the type, and indexing is checked.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Assigning a new value to a variable without mut | Compile error E0384: an immutable variable can be assigned only once |
Expecting mut to let the value change its type | E0308 mismatched types — a variable's type is fixed; only shadowing can change the type |
Reading a static mut or taking a reference to it like an ordinary variable | You need unsafe, and in edition 2024 even println! of such a variable does not compile; the replacement is atomics, Mutex, LazyLock |
| Testing arithmetic only in a debug build | In release, overflow does not panic but silently wraps: for u8, 255 + 1 gives 0 |
Narrowing a number with as | Data is lost without any error: 1000_i32 as u8 gives 232; u8::try_from gives you the check |
Indexing an array with an i32 or with an out-of-bounds number | E0277 — the index must be a usize; going out of bounds is an index out of bounds panic |
Why it matters in interviews
This block opens almost every Rust interview: the questions are simple, but the answers show whether the candidate got the idea of the language — explicitness and checks instead of silent surprises. The most frequent questions are the difference between shadowing and mut, between const and static, and how integers behave on overflow.
What interviewers usually check:
- Why
letis immutable by default and whatmutchanges. - How shadowing differs from changing a variable with
mut, and why it can change the type. - How
constdiffers fromstatic, and whystatic mutneedsunsafe. - What happens on overflow in debug and release builds, and which methods make the behaviour explicit.
- Which
ascasts lose data, and when to useFromorTryFrominstead. - How a tuple differs from an array, and what happens when an index goes out of bounds.