Vec — dynamic, heap-allocated, growable. Array — fixed-size, stack-allocated, copy.
Vec:
.push(), .pop(), .insert().1let mut v = Vec::new();2v.push(1);3v.push(2);4v.push(3);5println!("{:?}", v); // [1, 2, 3]6let v2 = vec![4, 5, 6]; // Vec from macro
Array:
Copy if elements are Copy.1let a: [i32; 3] = [1, 2, 3]; // Explicit type2let a = [0; 5]; // [0,0,0,0,0]3let first = a[0]; // 0
When to use:
Vec when the number of elements varies (most common).&[T]) to work with either generically.Common mistakes:
a.push(4) — not allowed.Vec<T> and [T] are different types.vec! macro returns Vec, not an array.