VecDeque::push_front, push_back, pop_front, pop_back
pub fn push_front(&mut self, value: T)
pub fn push_back(&mut self, value: T)
pub fn pop_front(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>Grow or shrink a double-ended queue at either end. Pops return Option and do not panic on an empty queue.
| Parameter | Description |
|---|---|
value | The element to insert at the front or back. |
Returns: The push methods return (). The pop methods return Some(value) or None when the queue is empty.
- From([2, 3]) starts with 2 at the front. After push_front(1) and push_back(4), pop_front yields 1 and pop_back yields 4, leaving [2, 3].
- make_contiguous can be used when you need a single &mut [T] view. This page does not restate slice methods.
use std::collections::VecDeque;
fn main() {
let mut q = VecDeque::from([2, 3]);
q.push_front(1);
q.push_back(4);
println!("front={:?} back={:?} mid={:?}", q.pop_front(), q.pop_back(), q);
}Expected output
front=Some(1) back=Some(4) mid=[2, 3]Related: BTreeMap and HashSet, Vec