Quick Ref: Type System ​
Updated Mar 2026Type Hierarchy ​
Scalar Types ​
| Type | Size | Range | Notes |
|---|---|---|---|
i8 | 1 byte | -128 to 127 | |
i16 | 2 bytes | -32,768 to 32,767 | |
i32 | 4 bytes | -2B to 2B | Default integer |
i64 | 8 bytes | -9.2E18 to 9.2E18 | |
i128 | 16 bytes | Huge | |
isize | Platform | Pointer-sized signed | For indexing |
u8 | 1 byte | 0 to 255 | Bytes |
u16 | 2 bytes | 0 to 65,535 | |
u32 | 4 bytes | 0 to 4B | |
u64 | 8 bytes | 0 to 18.4E18 | |
u128 | 16 bytes | Huge | |
usize | Platform | Pointer-sized unsigned | Array indexing |
f32 | 4 bytes | 6-7 decimal digits | |
f64 | 8 bytes | 15-16 decimal digits | Default float |
bool | 1 byte | true / false | |
char | 4 bytes | Unicode 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 suffixCompound 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 ​
| Type | Owned? | Mutable? | Use Case |
|---|---|---|---|
String | Yes | Yes (if mut) | Building/modifying strings |
&str | No | No | Function parameters, slices |
&'static str | No | No | String literals, constants |
Cow<'a, str> | Maybe | On write | Avoid 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"); }
}