Sub topic 17
Where tuples and arrays live in memory
This chapter assumes you can read basic Rust, know what a reference is, and have met tuples, arrays, slices and Vec. It explains where their values are stored and how large they are.
Stored inline, usually on the stack
Tuples and arrays hold their values directly. There is no pointer to a separate allocation. A local [i32; 4] is 16 contiguous bytes in the stack frame of the function that declares it. A local (i32, f64) is stored in that frame in the same way.
"Inline" means the value lives wherever its owner lives. If you put an array inside a Box, a struct, or a Vec<[i32; 4]>, the array's bytes sit inside that heap allocation or struct.
Array layout
An array of N items of type T takes exactly N * size_of::<T>() bytes. The items sit next to each other with no gaps and no header. Rust guarantees this. Indexing is therefore a base address plus index * size_of::<T>(), and a bounds check against N comes first.
Tuple layout
A tuple's fields are stored inline, but Rust does not promise their order in memory. The compiler may reorder fields to cut padding. Padding is unused bytes inserted so each field starts at an address its type allows.
use std::mem::{size_of, size_of_val};
fn main() {
println!("{}", size_of::<(u8, u32)>());
println!("{}", size_of::<(u8, u32, u8)>());
println!("{}", size_of::<[i32; 4]>());
println!("{}", size_of::<()>());
let array = [1i32, 2, 3, 4];
let slice: &[i32] = &array[1..];
println!("{}", size_of_val(&array));
println!("{}", size_of_val(slice));
println!("{}", size_of::<&[i32]>());
}On a typical 64-bit target this prints 8, 8, 16, 0, 16, 12, 16. Here is how to read those numbers:
(u8, u32)needs 5 bytes of data but takes 8, because theu32must start on a 4-byte boundary.(u8, u32, u8)also takes 8, because the compiler can put theu32first and the twou8values after it. A fixed-order layout would need 12 bytes. The exact numbers for tuples are not guaranteed, so never depend on them.()takes 0 bytes. It holds no data.size_of_val(slice)measures the 3 elements it views, not the slice reference.
Slices and references
A slice [T] has no size known at compile time, so you only use it behind a pointer. A &[T] is a fat pointer. It holds the address of the first element and the element count, which is 16 bytes on a 64-bit target. This stored length is what lets slice indexing do bounds checks.
Consequences
- Copying an array or tuple copies all its bytes. A large
[u8; 1_000_000]passed by value is a 1 MB copy, and a local one can overflow the stack. Use a reference, aBox, or aVecfor big data. - The length is part of the array's type, so the compiler knows the size and needs no heap allocation.
- A
Vec<T>is a pointer, length and capacity on the stack (24 bytes on 64-bit), with its elements contiguous on the heap.
Exercise
Why can [i32; 4] be a local variable with no heap allocation, while [i32] cannot be a local variable?
Show answer
[i32; 4] has a size known at compile time (16 bytes), so the compiler can reserve that space in the stack frame. [i32] has no known size, so it can only be used behind a pointer such as &[i32], which stores an address and a length.