Vec — dynamic, heap-allocated, growable. Array — fixed-size, stack-allocated, Copy.
Vec:
.push(), .pop(), .resize().Copy (owns heap data).1let mut v = Vec::new();2v.push(10);3v.push(20);4v.push(30);5println!("{:?}", v); // [10, 20, 30]6println!("{}", v.len()); // 378let v2 = vec![1, 2, 3]; // Macro syntax9let v3: Vec<i32> = (0..5).collect();
Array:
Copy if elements do.1let a: [i32; 4] = [1, 2, 3, 4];2let zeros = [0; 10]; // [0; 10]3println!("{}", a[2]); // 34println!("{}", a.len()); // 4
When to use:
Vec for most collection needs (default choice).&[T]) to abstract over both.Common mistakes:
vec! return type with arrays..as_slice() when a function expects &[T].