Programming Languages
Rust
Subjective
Oct 04, 2025
What is the difference between Vec and arrays in Rust?
Detailed Explanation
Vec vs Arrays:
• Arrays: fixed size, stack allocated, [T; N]
• Vec: dynamic size, heap allocated, Vec
• Arrays known at compile time
• Vec can grow and shrink
• Different indexing behavior
Example:
// Array
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let slice = &arr[1..3];
// Vec
let mut vec = Vec::new();
vec.push(1);
vec.push(2);
let element = vec.pop(); // returns Option
// Convert
let vec_from_array = arr.to_vec();
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts