Member-only story

4 Unpopular Data Structures That Every Rust Engineer Should Know About

BinaryHeap, VecDeque & more!

Dr. Ashish Bamania
3 min readMar 9, 2024
Generated with DALL-E 3

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

  1. 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)

--

--

Dr. Ashish Bamania
Dr. Ashish Bamania

Written by Dr. Ashish Bamania

🍰 I simplify the latest advances in AI, Quantum Computing & Software Engineering for you | 📰 Subscribe to my newsletter here: https://intoai.pub

Responses (2)