Rust’s Built‑In Types: Scalars, Compounds, Enums, and More

Quick reference to Rust’s core types: ints, floats, bool, char, tuples, arrays, slices, structs, enums, references, String, Option, Result, Vec, pointers.

Introduction

A recent query asked for an overview of the data types that Rust provides. The response covered Rust’s type system in depth, organizing the information into logical categories and illustrating each with concise tables and code snippets. Below is a polished blog‑post version of that answer, suitable for readers who want a quick yet comprehensive reference to Rust’s built‑in and commonly used types.


1. Scalar Types

Scalar types represent single, indivisible values.

Category Types Description
Integers i8, i16, i32, i64, i128, isize<br>u8, u16, u32, u64, u128, usize Signed and unsigned integers of various widths. isize/usize adapt to the target architecture (32‑ or 64‑bit).
Floats f32, f64 Single‑precision (f32) and double‑precision (f64). f64 is the default.
Boolean bool Either true or false.
Character char A Unicode scalar value (4 bytes). Examples: 'a', '🦀', '€'.

Key point: The default integer type is i32, and the default floating‑point type is f64.


2. Compound Types

Compound types combine multiple values into one logical unit.

2.1 Tuples

Fixed‑size collections that may hold values of different types.

let point: (i32, f64, u8) = (10, 3.14, 1);
let x = point.0; // Access first element
let y = point.1; // Access second element

2.2 Arrays

Fixed‑length sequences where every element has the same type.

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

For dynamically sized collections, Rust provides the vector type Vec<T> (part of the standard library).


3. Custom Data Types

Rust lets you define your own structured types.

3.1 Structs

Group related fields together.

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

3.2 Enums

Define a type that can be one of several variants, optionally carrying data.

enum Color {
    Red,
    Green,
    Blue,
}

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

Enums are the backbone of Rust’s pattern matching and are heavily used in error handling (Result) and optional values (Option).


4. Special Types

These types underpin Rust’s ownership, borrowing, and safety guarantees.

Type Purpose
References (&T, &mut T) Borrowing: immutable (&T) or mutable (&mut T) access without taking ownership.
Raw pointers (*const T, *mut T) Unsafe, C‑style pointers for low‑level interfacing.
String types String (owned, mutable, heap‑allocated) and &str (borrowed string slice).
Slices (&[T]) Borrowed view into an array, vector, or other contiguous collection.
Function pointers (fn) Storing a reference to a named function.
Unit type (()) Represents “no value”; functions that return nothing actually return ().
Never type (!) Indicates a function that never returns (e.g., loops forever or aborts).

5. Frequently Used Library Types

Although not primitive, these generic types dominate everyday Rust code.

  • Option<T> – Represents an optional value (Some(T) or None).
  • Result<T, E> – Encodes success (Ok(T)) or error (Err(E)), facilitating error handling.
  • Vec<T> – Growable, heap‑allocated list.
  • Box<T> – Heap allocation with deterministic deallocation.
  • Rc<T> – Single‑threaded reference‑counted pointer.
  • Arc<T> – Thread‑safe, atomic reference‑counted pointer.
  • Cell<T> / RefCell<T> – Enable interior mutability in safe code.

These types are built on top of the core primitive types and illustrate Rust’s emphasis on safety, composability, and zero‑cost abstractions.


6. Summary

Rust’s type system can be visualized as follows:

  • Scalar – simple, single values (i32, f64, bool, char).
  • Compound – collections of values ((T1, T2, …), [T; N]).
  • Custom – programmer‑defined structures (struct, enum).
  • Special – ownership‑related constructs (&T, &mut T, String, slices, raw pointers, unit/never types).
  • Standard‑library generic – reusable containers and error‑handling abstractions (Option, Result, Vec, Box, Rc, Arc, Cell).

Together, they give Rust a robust, expressive, and memory‑safe foundation that lets developers write high‑performance code without sacrificing safety.

Made with chatblogr.com