Skip to content

Quick Ref: Type System ​

Updated Mar 2026

Type Hierarchy ​

Scalar Types ​

TypeSizeRangeNotes
i81 byte-128 to 127
i162 bytes-32,768 to 32,767
i324 bytes-2B to 2BDefault integer
i648 bytes-9.2E18 to 9.2E18
i12816 bytesHuge
isizePlatformPointer-sized signedFor indexing
u81 byte0 to 255Bytes
u162 bytes0 to 65,535
u324 bytes0 to 4B
u648 bytes0 to 18.4E18
u12816 bytesHuge
usizePlatformPointer-sized unsignedArray indexing
f324 bytes6-7 decimal digits
f648 bytes15-16 decimal digitsDefault float
bool1 bytetrue / false
char4 bytesUnicode scalar value

Integer Literals ​

rust
let decimal = 98_222;      // Underscores for readability
let hex = 0xff;            // Hexadecimal
let octal = 0o77;          // Octal
let binary = 0b1111_0000;  // Binary
let byte = b'A';           // Byte (u8 only)
let typed = 42u64;         // Type suffix

Compound Types ​

rust
// Tuples — fixed size, heterogeneous
let tup: (i32, f64, bool) = (500, 6.4, true);
let (x, y, z) = tup;        // Destructuring
let first = tup.0;          // Index access

// Arrays — fixed size, homogeneous
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let zeros = [0; 10];        // [0, 0, 0, ..., 0] (10 elements)
let first = arr[0];         // Index access
let slice = &arr[1..3];     // Slice: [2, 3]

// Unit type — zero-size
let unit: () = ();

String Types ​

TypeOwned?Mutable?Use Case
StringYesYes (if mut)Building/modifying strings
&strNoNoFunction parameters, slices
&'static strNoNoString literals, constants
Cow<'a, str>MaybeOn writeAvoid cloning until needed
rust
let literal: &str = "hello";                    // &'static str
let owned: String = String::from("hello");      // String
let borrowed: &str = &owned;                    // &str from String
let slice: &str = &owned[0..3];                 // &str slice "hel"

Type Conversions ​

rust
// as — primitive casting (may truncate!)
let x: i64 = 42;
let y: i32 = x as i32;
let z: f64 = x as f64;

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

// TryFrom/TryInto — fallible conversion
let n: i32 = 256;
let byte: Result<u8, _> = u8::try_from(n);  // Err: 256 doesn't fit

// parse — string to number
let n: i32 = "42".parse().unwrap();
let n = "42".parse::<i32>().unwrap();  // Turbofish

// to_string — anything with Display
let s: String = 42.to_string();

Generics ​

rust
// Generic function
fn largest<T: PartialOrd>(list: &[T]) -> &T {
    let mut largest = &list[0];
    for item in &list[1..] {
        if item > largest {
            largest = item;
        }
    }
    largest
}

// Generic struct
struct Point<T> {
    x: T,
    y: T,
}

// Generic with different types
struct Pair<T, U> {
    first: T,
    second: U,
}

// Generic impl
impl<T: std::fmt::Display> Point<T> {
    fn print(&self) {
        println!("({}, {})", self.x, self.y);
    }
}

// Specialized impl (only for f64)
impl Point<f64> {
    fn distance_from_origin(&self) -> f64 {
        (self.x.powi(2) + self.y.powi(2)).sqrt()
    }
}

Type Aliases ​

rust
// Simple alias
type Kilometers = i32;

// Complex type alias (reduces verbosity)
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
type Callback = Box<dyn Fn(i32) -> i32>;

// Usage
fn run(f: Callback) -> Result<i32> {
    Ok(f(42))
}

Never Type and Infallible ​

rust
// ! (never type) — function that never returns
fn infinite_loop() -> ! {
    loop {
        // Never returns
    }
}

fn exit_now() -> ! {
    std::process::exit(1);
}

// Used in match arms that diverge
let guess: u32 = match "42".parse() {
    Ok(num) => num,
    Err(_) => panic!("Not a number"),  // panic! returns !
};

PhantomData ​

rust
use std::marker::PhantomData;

// PhantomData tells compiler about unused type params
struct Slice<'a, T: 'a> {
    start: *const T,
    end: *const T,
    phantom: PhantomData<&'a T>,  // "acts like it holds &'a T"
}

// Type-state pattern with PhantomData
struct Locked;
struct Unlocked;

struct Door<State> {
    _state: PhantomData<State>,
}

impl Door<Locked> {
    fn unlock(self) -> Door<Unlocked> {
        Door { _state: PhantomData }
    }
}

impl Door<Unlocked> {
    fn lock(self) -> Door<Locked> {
        Door { _state: PhantomData }
    }
    fn open(&self) { println!("Door opened"); }
}

Built with VitePress | Rust SOP Documentation