Variable Types in Rust

Rust is a statically typed language, which means the compiler must know the types of all variables at compile time. Let’s explore the type system.

Scalar Types

Integers

Rust has both signed and unsigned integers of various sizes:

Unsigned Signed Size
u8 i8 8-bit
u16 i16 16-bit
u32 i32 32-bit
u64 i64 64-bit
u128 i128 128-bit
usize isize pointer-sized

usize and isize have the size of the system’s default pointer—usually 32 or 64 bits depending on your architecture.

Floating Point

Type Size
f32 32-bit
f64 64-bit

Declaration Examples

let x: i32 = 3;
let y: f64 = 3.14;
let z = 3.14_f32;  // Type suffix

Booleans

let flag: bool = true;

Characters

let my_letter = 'a';
let my_emoji = '💪';

A character in Rust is always 4 bytes (32-bit), supporting the full Unicode range. This means an array of characters is essentially a UTF-32 string.

Compound Types

Tuples

Tuples group multiple values of potentially different types:

let info = (1, 33.3, 999);

Access tuple fields with dot syntax:

let jets = info.0;
let fuel = info.1;
let ammo = info.2;

Or use destructuring:

let (jets, fuel, ammo) = info;

Tuples can hold up to 12 elements.

Arrays

Arrays store multiple values of the same type with a fixed length:

let buf = [1, 2, 3];
let zeros = [0; 3];           // [0, 0, 0]
let typed: [u8; 3] = [1, 2, 3];

Arrays in Rust are stack-allocated and have a maximum size of 32 elements. For larger or dynamically-sized collections, use Vec<T>.