What is the difference between Vec and VecDeque

Vec is a single-ended growable array, while VecDeque is a double-ended queue optimized for operations at both ends.

Vec is a growable array optimized for fast access and appending at the end, while VecDeque is a double-ended queue optimized for fast insertion and removal at both the front and back. Use Vec for standard lists and VecDeque when you need a queue or stack that operates efficiently on both ends.

use std::collections::VecDeque;

// Vec: Fast push/pop at the end
let mut v = Vec::new();
v.push(1);
let last = v.pop(); // Removes from end

// VecDeque: Fast push/pop at both ends
let mut dq = VecDeque::new();
dq.push_front(1); // Fast at front
dq.push_back(2);  // Fast at back
let first = dq.pop_front(); // Removes from front