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/usizedepend 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β eithertrueorfalse
β 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]
- e.g.,
β 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 (SomeorNone)Result<T, E>β Represents operation results (success or error)Box<T>β Smart pointer for heap allocationRc<T>β Reference-counted smart pointerArc<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.