Rustlers Atom 1.3 - Composite Types: tuple, array, slice
April 18, 2026•371 words
Rust’s composite types let you pack values together and work with them as a unit. They are minimal but powerful, and you’ll use them everywhere.
Tuples
A tuple can hold multiple values of different types.
fn main() {
let point: (i32, i32, i32) = (3, 4, 5);
// Destructure
let (x, y, z) = point;
println!("x = {}, y = {}, z = {}", x, y, z);
// Access by index
println!("First: {}", point.0);
println!("Second: {}", point.1);
// Function returning a tuple
fn min_max(values: &[i32]) -> (i32, i32) {
let mut min = values[0];
let mut max = values[0];
for &v in values {
if v < min { min = v; }
if v > max { max = v; }
}
(min, max)
}
let nums = [10, 2, 300, -5, 42];
let (lo, hi) = min_max(&nums);
println!("min = {}, max = {}", lo, hi);
}
A tuple is just a lightweight container. Even () (the unit type) is a tuple with zero elements.
Arrays
An array has fixed size and homogeneous elements.
fn main() {
let months = ["Jan", "Feb", "Mar", "Apr"];
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
println!("First month = {}", months[0]);
println!("Array length = {}", numbers.len());
// Filled array
let zeros = [0; 8];
println!("zeros = {:?}", zeros);
// Iterating
for m in months {
println!("{}", m);
}
}
Array size is part of the type. [i32; 3] and [i32; 4] are incompatible.
Slices
A slice borrows a view into contiguous memory.
fn main() {
let arr = [10, 20, 30, 40, 50];
// Borrow the whole array
let slice: &[i32] = &arr;
println!("Slice = {:?}", slice);
// Borrow a section
let middle = &arr[1..4]; // [20, 30, 40]
println!("Middle = {:?}", middle);
// Mut slice
let mut data = [1, 2, 3, 4];
let slice_mut: &mut [i32] = &mut data[1..3];
slice_mut[0] *= 10;
println!("Mutated = {:?}", data); // [1, 20, 3, 4]
}
Slices don’t own data; they borrow it. This is the foundation for Rust’s safe handling of collections.