Data types in Rust

Rust is a statically-typed language with a rich set of data types. They are broadly divided into scalar and compound types. Additionally, Rust provides custom and special types. Here's a breakdown:


πŸ”Ή 1. Scalar Types

Represent single values.

βœ… Integers

Signed and unsigned integers of various sizes:

Size Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
Arch-specific isize usize
  • Default integer type: i32
  • isize/usize depend on the architecture (32-bit or 64-bit)

βœ… Floating-Point Numbers

Type Precision
f32 32-bit (single precision)
f64 64-bit (double precision) β€” default

βœ… Booleans

  • bool β€” either true or false

βœ… Characters

  • char β€” represents a Unicode scalar value (4 bytes), e.g., 'a', '€', '⚑'

πŸ”Ή 2. Compound Types

Group multiple values into one type.

βœ… Tuples

Fixed-size collection of values of possibly different types:

let tup: (i32, f64, u8) = (500, 6.4, 1);
let x = tup.0; // Access via index

βœ… Arrays

Fixed-length sequences of the same type:

let arr: [i32; 5] = [1, 2, 3, 4, 5];
let first = arr[0];

Note: Use Vec<T> (vectors) for dynamically-sized arrays.


πŸ”Ή 3. Custom Data Types

βœ… Structs

User-defined types that group related data:

struct Point {
    x: i32,
    y: i32,
}

βœ… Enums

Types that can have a fixed set of variants:

enum Color {
    Red,
    Green,
    Blue,
}

Can also carry data:

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
}

πŸ”Ή 4. Special Types

βœ… References (&T) and Pointers

  • &T: Immutable reference
  • &mut T: Mutable reference
  • Raw pointers: *const T, *mut T (unsafe)

βœ… String Types

  • String: Growable, heap-allocated string
  • &str: String slice (borrowed reference to string data)

βœ… Slices

  • &[T]: Reference to a contiguous sequence in a collection
    • e.g., &arr[1..3]

βœ… Function Types

  • fn β€” function pointers or function types
fn add(x: i32, y: i32) -> i32 { x + y }
let f: fn(i32, i32) -> i32 = add;

βœ… unit Type (())

The empty tuple, represents "no value" or "nothing":

  • Functions that don’t return a value return ().

βœ… Never Type (!)

Used for functions that never return:

fn loop_forever() -> ! {
    loop {}
}

πŸ”Ή 5. Generic and Trait-Based Types

Rust supports:

  • Generic types (Vec<T>, Option<T>, Result<T, E>)
  • Trait objects (Box<dyn Trait>)
  • Closures (with unique, compiler-generated types)

Common Standard Library Types

Even though not primitive, these are widely used:

  • Option<T> β€” Represents optional values (Some or None)
  • Result<T, E> β€” Represents operation results (success or error)
  • Box<T> β€” Smart pointer for heap allocation
  • Rc<T> β€” Reference-counted smart pointer
  • Arc<T> β€” Atomically reference-counted (thread-safe)
  • Cell<T>, RefCell<T> β€” Interior mutability

Summary

Rust’s type system includes:

  • Scalar: i32, f64, bool, char
  • Compound: tuples, arrays
  • Custom: structs, enums
  • Special: references, slices, strings, function pointers
  • Library types: Option, Result, Vec, String, etc.

This strong, expressive type system ensures memory safety and performance at compile time.