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:
Let us discuss each category in detail.
Here, we will discuss the three types of sequence collections in Rust:
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:
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:
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:
// push back
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:
This should return as shown below:
[2, 1, 4]
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.
The previous example should return as follows:
To remove elements from a linked list, use the pop_front and pop_back methods:
The output is as shown:
[2, 3, 4] // after
The second category of Rust collections in Maps, and these include:
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:
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:
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:
The next category of Rust collections is sets. These types are derived from the set theory, and they include:
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.
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:
There is only one type in the Misc collections.
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.
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.