I Thought Arrays, Slices, and Vectors in Rust Were the Same Thing. I Was Wrong
I wrote a function that took a &Vec<i32> because that’s what I had sitting in a variable, and a colleague’s review comment said to take a &[i32] instead. My first thought was that this was the same advice as &str versus &String all over again, and it mostly is, but there’s a third player in this specific family that string types don’t have, and the moment I actually needed a fixed-size buffer with no heap allocation at all, I realized I’d been treating three genuinely different tools as if they were one.
An array is a size, not just a sequence
[T; N] is a fixed number of elements of the same type, and that number is part of the type itself, known at compile time, not a property you check at runtime.
let rgb: [u8; 3] = [255, 0, 128];
let rgb: [u8; 3] = [255, 0, 128];
[u8; 3] and [u8; 4] are not almost the same type with different lengths. They are two completely different types, in the same way i32 and i64 are different types, and a function written to accept one will not accept the other without you saying so explicitly. This is what makes an array the right tool the moment you know, structurally, that something will always have exactly this many elements, an RGB color, a fixed-size checksum, a chess board’s rank. An array stores its elements inline, with its size known at compile time, and doesn’t require a separate allocation just to hold them. Where that inline storage actually ends up, the stack, inside a struct, inside a static, depends on where the array itself lives, but it never needs a heap buffer of its own the way the next type does.
A slice is a view, not a container
&[T] doesn’t own anything. It’s a fat pointer, an address plus a length, pointing into a sequence that lives somewhere else, an array, a Vec, or another slice.
i32 {
values.iter().sum()
}
let arr: [i32; 4] = [1, 2, 3, 4];
let vec: Vec = vec![1, 2, 3, 4];
sum(&arr); // an array coerces to a slice automatically
sum(&vec); // so does a Vec
sum(&vec[1..3]); // and so does a slice of a slice” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn sum(values: &[i32]) -> i32 {
values.iter().sum()
}
let arr: [i32; 4] = [1, 2, 3, 4];
let vec: Vec<i32> = vec![1, 2, 3, 4];
sum(&arr); // an array coerces to a slice automatically
sum(&vec); // so does a Vec
sum(&vec[1..3]); // and so does a slice of a slice
fn sum(values: &[i32]) -> i32 {
values.iter().sum()
}
let arr: [i32; 4] = [1, 2, 3, 4];
let vec: Vec<i32> = vec![1, 2, 3, 4];
sum(&arr); // an array coerces to a slice automatically
sum(&vec); // so does a Vec
sum(&vec[1..3]); // and so does a slice of a slice
This is the actual reason the review comment mattered. A function that asks for &[i32] accepts an array, a Vec, or a slice of either, all through the same automatic coercion. A function that specifically asks for &Vec<i32> only ever accepts a Vec, which is strictly less flexible for no benefit, exactly the same shape of advice as taking &str over &String, just one level up, on the collection itself rather than on text.
A Vec is the one that actually owns growable memory
Vec<T> is the one of the three that owns its contents and can grow or shrink at runtime. It owns a dynamically sized buffer, normally stored on the heap, and manages that allocation as its capacity changes, reallocating to a larger buffer whenever it outgrows the space it currently has.
<button data-code="let mut scores: Vec = Vec::new();
scores.push(10);
scores.push(20); // may trigger a reallocation if capacity was exceeded” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-1/2 -translate-y-1/2 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
let mut scores: Vec<i32> = Vec::new();
scores.push(10);
scores.push(20); // may trigger a reallocation if capacity was exceeded
let mut scores: Vec<i32> = Vec::new();
scores.push(10);
scores.push(20); // may trigger a reallocation if capacity was exceeded
This is the tool for when you genuinely don’t know the size ahead of time, or when the collection needs to grow and shrink over its lifetime, and it comes with a real cost: the possibility of reallocating as it grows, a cost an array doesn’t have because its size was fixed from the beginning. Vec actually tracks two separate numbers worth knowing apart, length and capacity. Length is how many elements are actually stored right now. Capacity is how many the current buffer could hold before another allocation becomes necessary.
let mut scores = Vec::with_capacity(100);
// len is 0, capacity is 100
// pushing the first 100 elements won't require reallocation
let mut scores = Vec::with_capacity(100);
// len is 0, capacity is 100
// pushing the first 100 elements won't require reallocation
If you already know roughly how large a Vec will grow, reserving that capacity up front avoids the repeated reallocations that happen when a Vec grows one push at a time from nothing.
Putting the three side by side
An array is the right choice when the length is fixed and known up front, and you want it stored inline with no separate allocation. A Vec is the right choice when the length genuinely varies at runtime and you need to own that growth yourself. A slice is usually not the thing you allocate and own, it’s the view you pass around when you only need access to some contiguous elements, whether they came from an array or a Vec.
String {
format!("{} items, first is {:?}", data.len(), data.first())
}
describe(&[1, 2, 3]); // array literal
describe(&vec![1, 2, 3]); // Vec
describe(&[1, 2, 3][..2]); // slice of a slice” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn describe(data: &[i32]) -> String {
format!("{} items, first is {:?}", data.len(), data.first())
}
describe(&[1, 2, 3]); // array literal
describe(&vec![1, 2, 3]); // Vec
describe(&[1, 2, 3][..2]); // slice of a slice
fn describe(data: &[i32]) -> String {
format!("{} items, first is {:?}", data.len(), data.first())
}
describe(&[1, 2, 3]); // array literal
describe(&vec![1, 2, 3]); // Vec
describe(&[1, 2, 3][..2]); // slice of a slice
Same function, three different underlying owners, because the function only ever asked to look, not to own.
A quick way to picture all three at once
An array is inline storage of a fixed size, the elements sit right where the array itself sits, nothing else attached. A Vec is a small owned record, a pointer, a length, and a capacity, pointing out to a separate buffer it manages on the heap. A slice is that same shape, a pointer and a length, except it owns nothing behind that pointer at all, it’s borrowing someone else’s storage, whether that storage happens to be an array’s inline bytes or a Vec‘s heap buffer.
Where this connects to const generics
The N in [T; N] isn’t decoration, it’s a const generic parameter, the same mechanism that lets a type carry a compile-time value rather than just a compile-time type. [i32; 3] and [i32; 4] are different types for the same underlying reason Vec<i32> and Vec<u8> are different types, both are generic over something, just generic over a number in one case and a type in the other. Writing a function generic over array length reaches for exactly this mechanism directly:
<button data-code="fn sum(data: [i32; N]) -> i32 {
data.iter().sum()
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-1/2 -translate-y-1/2 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fn sum<const N: usize>(data: [i32; N]) -> i32 {
data.iter().sum()
}
fn sum<const N: usize>(data: [i32; N]) -> i32 {
data.iter().sum()
}
This one function works for [i32; 3], [i32; 10], any length at all, while the array’s actual length stays baked into its type the whole way through. It’s the same const generics territory this series has already covered from the other direction, generics as a type-level tool rather than only a value-level one.
What actually changed once I saw the three as separate tools
The advice “take a slice, not an owned collection” is correct, but it undersold the actual lesson, which is that these three types are answering three different questions, not offering three levels of the same feature. An array answers “how many, always, no exceptions.” A Vec answers “however many, and I’ll own the consequences of that changing.” A slice answers “let me look, I don’t need to know or care which of the other two you actually built.” Once I stopped treating them as interchangeable synonyms with different performance characteristics, choosing between them stopped being a style question and started being a design one, decided by what the data actually was, not by which one happened to be sitting in scope when I wrote the function signature.
If you’re prepping for interviews or just want the fast reference: Rust vs. Java — The Data Structures Cheat Sheet is free — Big-O for every structure, the full 55-problem interview index by chapter, and the three rules that separate a senior answer from a junior one.
If you’re curious how these same ownership and collection questions play out on the Java side, that’s the whole premise of the book I’ve been writing: every classic data structure built once in Rust and once in Java, side by side, so you can see exactly where each language’s rules force a different decision. Ownership vs. Reference: Data Structures and Algorithms in Rust and Java — $9 · Free preview
For the Rust foundations that series builds on: Zero to Rust: A Systems Programmer’s Field Guide — $7 · Free 20-page sample


