variables and types

Immutability by default

let x = 5;
x = 6;

That fails let bindings are immutable unless you write mut

let mut x = 5;
x = 6;

That is the deliberate friction Most variables do not need to change Marking the ones that do makes the intent visible

Scalars

i8 through i128, u8 through u128, isize and usize f32, f64 bool char Use usize for indices and sizes Use fixed width types when a format depends on exact size

Compounds

let ports: [i32; 3] = [22, 80, 443];
let pair: (String, i32) = (String::from("0x1RIS"), 443);

Arrays have fixed length Tuples group different types Vectors are the dynamic version and live in std::vec

Shadowing

let x = 5;
let x = x + 1;
let x = "now a string";

Shadowing creates a new binding with the same name It is not mutation Use it to transform a value through stages without inventing new names

Next

Ownership in 03 ownership

Posted