Member-only story
4 Unpopular Data Structures That Every Rust Engineer Should Know About
BinaryHeap, VecDeque & more!
Have you used Vec
and String
in Rust?
These are two quite common data structures that are used all the time by developers.
But what if I tell you that Rust has a standard collections library that offers much more?
Let’s talk about 4 interesting ones from this!
1. BinaryHeap
BinaryHeap<T>
is a binary tree that maintains the max-heap ordering property.
This means that in a Binary Heap, each parent node is greater than or equal to its child nodes.
This means that the maximum node in the heap will always be at the root and it can be accessed in a constant time operation.
This makes it possible to implement a Priority Queue using this data structure.
Common Operations
- Creating a Binary Heap
use std::collections::BinaryHeap;
let mut heap: BinaryHeap<i32> = BinaryHeap::new();
2. Adding Elements
heap.push(1);
heap.push(2);
heap.push(3);
//Time complexity: O(1)