Originally published on php2rust.dev — part of the PHP to Rust course. View this lesson on the original site.
Compound types bundle more than one value into one typed value. Where a scalar is a single integer, float, bool, or char, a compound value is “several things that travel together”, still one binding in Rust, but with structure inside (positions, fixed shape, or both).
This section focuses on Rust’s two built-in compound types that are mostly stack-friendly and fixed-size in their type:
- Tuples, a fixed number of values in order; each slot can be a different type (e.g.
(i32, f64, bool)). - Arrays, a fixed number of values in order; every element has the same type (e.g.
[i32; 5]).
What “sized at compile time in the type” means
The compiler knows the shape before your program runs. That shape is written into the type name, so mismatches are caught when you compile, not as a surprise at runtime.
- Tuples: The type is literally the list of slot types in order.
(i32, i32)is “a pair ofi32s”;(i32, f64, &str)is “three slots of those types.” The arity (how many values) is fixed: a(i32, i32)is not the same type as(i32, i32, i32). There is no.push(), you don’t “add another field” to an existing tuple value in place. - Arrays: The type is
[T; N], element typeTand exact countN.[i32; 3]and[i32; 4]are different types. The length is part of the type, so the compiler knows how much memory the array uses on the stack and can check indices. Again, no growing in place.
Can you add more elements to a Rust array? Not to a [T; N], you cannot append or change its length. You may overwrite slots (e.g. arr[2] = 99 on a mutable array), but it still has exactly N elements. To grow or shrink at runtime, use Vec<T> (or another growable collection), not a fixed-size array.
“You can’t append … without getting a new type” means: if you need one more element, you don’t mutate the same value into a longer tuple/array, you build a different value whose type reflects the new length (or you switch to a growable type). In PHP you might write $arr[] = 42; and the array “just gets longer”; in Rust, [i32; 5] stays five i32s forever. For variable length, Rust uses Vec<T> and slices &[T] (later in the course). Structs and enums are other compound types with their own fixed shapes, you’ll see those separately.
PHP has a single array type that covers a lot of ground: it can act like a zero-indexed list, an associative map ('x' => 10), or a mix; keys can be integers or strings; values can be mixed types; the structure can grow and shrink at runtime. There is no separate tuple type, you’d use a short indexed array, a small object, or named keys instead. That flexibility is convenient in PHP; in Rust, different shapes are different types, and the compiler checks length, indexing, and element types.
Rough mental map
| Idea | Rust (this chapter) | PHP |
|---|---|---|
| Fixed bundle, mixed types | Tuple (T, U, …) |
Indexed array with mixed values, or an object |
| Fixed bundle, same type, known length | Array [T; N] |
Indexed array [1, 2, 3] (length not in the type system) |
| Map / record by name | Struct (later) or tuple awkwardly | Associative array ['x' => 10] |
| Growable list | Vec<T> (later) |
$arr[] = x; |
Rust compound types
fn main() {
// Tuple: fixed-length, mixed types
let point: (i32, i32) = (10, 20);
let mixed: (i32, f64, &str) = (1, 3.14, "hello");
// Array: fixed-length, same type
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
println!("point={:?}, mixed={:?}, numbers={:?}", point, mixed, numbers);
}
PHP "compound" types
PHP's main compound type is the array (list or map):
<?php
// Indexed array (list-like)
$numbers = [1, 2, 3, 4, 5];
// Associative array (map-like)
$point = ['x' => 10, 'y' => 20];
// Mixed, PHP arrays are flexible
$mixed = [1, 3.14, "hello"];
PHP arrays are dynamic in length and can mix types. There is no built-in tuple type.
Comparison
- Rust tuples: Fixed length and field types in the type; heterogeneous;
.0/.1/ … (and some indexing patterns). - Rust arrays: Fixed length
Nand element typeTin[T; N]; homogeneous; index[i]with bounds checks. - PHP arrays: One container type; dynamic length; mixed value types; integer or string keys; ordered map semantics.
References
What's next
Next: Tuples.
From PHP to Rust — Rust Syntax and Functions