Collections are very useful when implementing flexible and generic data storage. Most collections can shrink or grow in the program.
Let us explore various data structures in the Rust programming language and how to perform the basic operations.
Here, we have the four major categories for the Rust collections:
- Sequences
- Maps
- Sets
- Misc
Let us discuss each category in detail.
Sequence Collections
Here, we will discuss the three types of sequence collections in Rust:
- Vec
- VecDeque
- LinkedList
Vec
A vec or vector is a contiguous growable array that stores values in a list, one after another in the memory.
To create a new empty vector, use the new method as shown below:
Then, you can add elements to the vector using the push method:
vec.push(2);
To print a vector, use the debug trait as provided below:
Remove the element in a vector using the remove method and the index of the element to remove, as shown below:
println!("{:?}", vec);
VecDeque
A VecDeque or double-ended vector is a non-contiguous growable ring buffer. We can create a new empty buffer using the new method as provided below:
let mut deque = VecDeque::new();
The VecDeque uses push_front() and push_back() methods to add elements to the front or back of the Deque:
deque.push_front(2);
deque.push_front(3);
// push back
deque.push_back(4);
deque.push_back(5);
To print the elements of the VecDeque, use the debug trait:
To remove elements from a VecDeque, use the pop_front() and pop_back() methods to remove an element from the front and back of the Deque respectively.
The following example is provided below:
deque.pop_front();
println!("{:?}", deque);
This should return as shown below:
[2, 1, 4]
LinkedList
This is a doubly linked list with owned nodes. It is useful when you need a vector or Deque of unknown size.
To create a new empty LinkedList, use the following:
let mut lnk = LinkedList::new();
We use the push_front() and push_back() methods to add elements to the front and back of a linked list, respectively.
For example:
lnk.push_front(3);
lnk.push_front(2);
lnk.push_front(1);
lnk.push_back(4);
lnk.push_back(5);
println!("{:?}", lnk);
The previous example should return as follows:
To remove elements from a linked list, use the pop_front and pop_back methods:
lnk.pop_front();
println!("{:?}", lnk);
The output is as shown:
[2, 3, 4] // after
Map Collections
The second category of Rust collections in Maps, and these include:
- HashMap
- BTreeMap
HashMap
A HashMap allows you to store mapping of key-value pairs. It uses a hashing function to determine how the keys and values are stored in memory. They are very useful when you need to store related values. It uses a key instead of an index to retrieve values.
To create a new HashMap, use the following syntax:
let mut map = HashMap::new();
To insert key-value pairs to a HashMap, use the following insert method:
map.insert(1, "React");
map.insert(3, "mithril");
map.insert(4, "Vue");
To print a HashMap, use the following:
This should return as shown below:
Keep in mind that the key and value can be any supported type.
To delete elements from a HashMap, use the remove()method as follows:
BTreeMap
You notice that a HashMap is not sorted. If you are looking for a sorted map, use the BTreeMap. Each element in the BTreeMap is stored in its own heap-allocated node.
To create a new BTreeMap, use the following:
let mut btree = BTreeMap::new();
To add elements, use the following insert method.
To delete an element, use the remove method as:
Set Collections
The next category of Rust collections is sets. These types are derived from the set theory, and they include:
- HashSets
- BTreeSet
HashSets
A HashSet is closely similar to a HashMap. This means it is a set form of a HashMap and does not allow duplicate values.
To create a new hashset, use the following syntax:
let mut set = HashSet::new();
Use the insert and remove methods to add and delete elements from a HashMap, respectively.
set.remove("a");
BTreeSet
This is a set implementation of a BTreeMap. We can create a new BTreeSet as shown below:
let mut set = BTreeSet::new();
Insert and remove elements as provided below:
set.remove("A");
Misc Collections
There is only one type in the Misc collections.
- BinaryHeap
BinaryHeap
Binary heap allows you to implement an ample binary tree. You can create a new binary heap as provided below:
let mut heap = BinaryHeap::new();
To add elements, use the following push method:
To remove a value, use the following pop method:
The pop method removes the greatest item in a heap.
Conclusion
This guide covers the popular and useful data structures and their basic operations in the Rust programming language. In addition, we discussed in detail the four major categories of the Rust collections, such as sequences, maps, sets, and misc. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.