Variable Types in Rust

rust programming types

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:

UnsignedSignedSize
u8i88-bit
u16i1616-bit
u32i3232-bit
u64i6464-bit
u128i128128-bit
usizeisizepointer-sized

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

Floating Point

TypeSize
f3232-bit
f6464-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>.