☼
⌃
☰

Rust Type System

Table of Contents

Rust's type system is static, strong, and deeply integrated with its ownership model. Every value has a type known at compile time. The compiler rejects type mismatches, null pointer dereferences, and data races — entire categories of bugs are impossible by construction. Type inference reduces annotation noise without sacrificing safety.

Related: Ownership in Rust Structs in Rust Traits in Rust Enums in Rust Type System Rust

Scalar Types

Type Description Size
i8 .. i128 Signed integers 1–16 bytes
u8 .. u128 Unsigned integers 1–16 bytes
isize / usize Pointer-sized signed/unsigned Architecture-dependent
f32 / f64 Floating point 4/8 bytes
bool true or false 1 byte
char Unicode scalar value 4 bytes
RUST
let x = 42;          // inferred as i32
let y: u8 = 255;
let pi: f64 = 3.14;
let flag = true;
let letter: char = 'é';   // Unicode — not just ASCII

Default integer type is i32; default float is f64. Use usize for indexing and collection sizes.

Compound Types

RUST
// Tuple — fixed size, mixed types
let pair: (i32, f64) = (42, 3.14);
let (x, y) = pair;        // destructuring
let first = pair.0;       // index access

// Array — fixed size, single type
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let zeros = [0; 100];     // 100 elements all set to 0
let third = arr[2];

Slice (&[T]) is a dynamically-sized view into a sequence — not stored directly, only as a reference.

Custom Types

Structs

See Structs in Rust for full detail.

RUST
struct User {
    name: String,
    age:  u32,
    active: bool,
}

// Tuple struct
struct Point(f64, f64);

// Unit struct (no fields)
struct Marker;

Enums

See Enums in Rust for full detail.

RUST
// Variants can hold associated data
enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
    Triangle { base: f64, height: f64 },
}

// Option<T> — replaces null
let name: Option<String> = Some("Alice".to_string());
let missing: Option<String> = None;

// Result<T, E> — replaces exceptions
let parsed: Result<i32, _> = "42".parse();

Type Inference

RUST
// Compiler infers type from context
let x = 5;                        // i32
let v = vec![1, 2, 3];            // Vec<i32>
let doubled: Vec<i32> = v.iter().map(|x| x * 2).collect();
// collect() needs help — the annotation on doubled tells it what to produce

Type inference works across expression boundaries — the compiler traces types through chains of method calls. Explicit annotation is only required when inference is ambiguous.

Ownership and the Type System

The type system encodes ownership and borrowing directly:

Type Meaning
T Owned value
&T Shared (immutable) reference
&mut T Exclusive (mutable) reference
Box<T> Heap-allocated owned value
Rc<T> Reference-counted shared ownership
Arc<T> Atomic reference-counted (thread-safe)

The compiler enforces: at any time, either one mutable reference or any number of immutable references may exist — never both. This is checked at compile time, not runtime.

See Ownership in Rust for the full model.

Type Conversion

RUST
// as — numeric casts (may truncate)
let x: i32 = 300;
let y = x as u8;   // 44 — wraps around

// From/Into — safe, infallible conversion
let s = String::from("hello");
let s: String = "hello".into();

// TryFrom/TryInto — fallible conversion
use std::convert::TryFrom;
let small = i8::try_from(300_i32);   // Err — doesn't fit

// parse() — string to numeric
let n: i32 = "42".parse().unwrap();
let n: Result<i32, _> = "abc".parse();   // Err

Tips

  • Prefer i32 for general integers unless you have a specific reason — it is the default and most ergonomic to work with
  • usize is required for indexing and lengths — mismatch with i32 is a frequent source of cast noise in Rust code
  • Option eliminates null pointer errors at the type level — a value is either Some(T) or None; you cannot use it without handling both cases
  • Result eliminates unchecked exceptions at the type level — errors are values, not surprises
  • Use as casts sparingly — they can silently truncate; TryFrom is safer when the conversion might fail