VecDeque, BinaryHeap & LinkedList
VecDeque is a double-ended queue. BinaryHeap is a priority queue (max-heap). LinkedList is rarely the right choice in Rust.
Search across all documentation pages
VecDeque is a double-ended queue. BinaryHeap is a priority queue (max-heap). LinkedList is rarely the right choice in Rust.
use std::collections::{BinaryHeap, VecDeque};
fn main() {
let mut q = VecDeque::from([1, 2]);
q.push_front(0);
let mut heap = BinaryHeap::from([1, 3, 2]);
assert_eq!(heap.pop(), Some(3));
}When to reach for this: BFS queues, schedulers, top-K problems - avoid LinkedList unless you have measured need.
use std::collections::BinaryHeap;
#[derive(Eq, PartialEq)]
struct Task { priority: u8, name: &'static str }
impl Ord for Task {
fn cmp(&self, o: &Self) -> std::cmp::Ordering {
self.priority.cmp(&o.priority)
}
}
impl PartialOrd for Task { fn partial_cmp(&self, o: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(o)) } }
fn main() {
let mut h = BinaryHeap::new();
h.push(Task { priority: 1, name: "low" });
h.push(Task { priority: 10, name: "high" });
println!("{}", h.pop().unwrap().name);
}What this demonstrates:
Ord for heap orderingVecDeque for FIFO with front/back opsLinkedList allocates per node - poor cache locality. Prefer Vec or VecDeque unless intrusive list required.
BinaryHeap::peek reads max without pop. VecDeque::make_contiguous for slice access.
VecDeque. Fix: Benchmark before using.Ord or use std::cmp::Reverse.Ord.None.| Alternative | Use When | Don't Use When |
|---|---|---|
Vec + sort | Small top-K | Streaming large K |
priority-queue crate | Extra features | Std enough |
crossbeam deque | Work-stealing | Simple queue |
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026