Skip to main content
Be Bitwise
RustRust Basics & Ownership

Variables & Mutability

Rust flips the default most languages assume: immutable until proven otherwise. Want to mutate? You ask for it explicitly with mut.

Immutable by Default

rust
let x = 5;
// x = 6; // ERROR!

Treat that as a feature, not a limitation. When you read let x = 5;, you know x will always be 5 in its scope. No need to trace through code to find where it might change.

Mutable Variables

rust
let mut count = 0;
count += 1;
count += 1;
println!("{}", count); // 2

mut doubles as documentation. Reading let mut count tells the next person — possibly future you — that this binding will move.

Shadowing

Rust permits shadowing — re-binding the same name to a fresh variable:

rust
let x = 5;
let x = x + 1; // new x, shadows the old one
let x = x * 2; // shadows again
println!("{}", x); // 12

Shadowing isn't mutation:

  • Shadowing introduces a brand new variable, possibly with a different type.
  • Mutation rewrites the existing variable in place.
rust
let spaces = " "; // &str
let spaces = spaces.len(); // usize - different type! OK with shadowing
Key Concept

Shadowing lets you keep the same name while morphing the value — types included. The classic use: parsing input with let guess: u32 = guess.trim().parse().unwrap();. Before the line, guess is a String; after, it's a u32. Same name, different type, no mutation.

Constants

Constants are immutable, always typed, and live anywhere from global scope to inside a function body:

rust
const MAX_POINTS: u32 = 100_000;
const PI: f64 = 3.14159265358979;

The compiler inlines them at every call site, so a const is closer to a #define with type-checking than a runtime variable.

Numeric Types

Rust has explicit integer sizes:

TypeSizeRange
i88 bits-128 to 127
i1616 bits-32,768 to 32,767
i3232 bits~-2 billion to ~2 billion
i6464 bitsvery large
u88 bits0 to 255
u3232 bits0 to ~4 billion
usizepointer sizeused for indexing

Default integer literal is i32, default float is f64. Type inference picks those when nothing else nails it down.

A fixed width means a fixed ceiling. A plain rustc main.rs (which is what the runner does) produces a debug build, and debug builds panic on integer overflow. Release builds (rustc -O or cargo build --release) wrap silently unless you switch overflow checks back on. When wrapping or clamping is what you want, say so with the explicit methods, and remember that integer as casts never check: they just keep the low bits.

Those ranges come straight out of the bit patterns. Below is an i8 holding 127, its largest value: a zero sign bit and seven ones. Adding 1 carries all the way up and leaves 1000 0000, so press Clear and click just the leftmost bit. Those eight bits read as -128, which is exactly what wrapping_add(1) returns. Then press Fill: 0xFF is -1 as an i8 and 255 as a u8, and nothing in the bits says which.

27
26
25
24
23
22
21
20
Decimal127
Hex0x7F
Binary01111111
Try itPredict each line, then change 0..5 to 0..6 and run again: the debug build panics instead of wrapping.
Rust (1.40.0)

Tuples and Arrays

rust
let point: (f64, f64) = (3.0, 4.0);
println!("{}, {}", point.0, point.1);
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
println!("{}", numbers[0]);
// println!("{}", numbers[5]); // ERROR: out of bounds (a runtime index panics instead)

Hit an out-of-range index and Rust panics — controlled stop, stack trace, the lot. C would have happily handed you whatever bytes lived past the end. That bounds check is non-negotiable.