1. What Are Collections?
Collections are data structures that store multiple values together.
Unlike arrays, some collections can grow or shrink while the program is running.
Rust provides different collection types for different requirements.
Collection | Purpose |
|---|---|
Vector ( | Stores multiple values in a sequence |
HashMap ( | Stores data as key-value pairs |
HashSet ( | Stores unique values |
2. Understanding Vectors (Vec<T>)
A Vector is a collection that stores multiple values of the same data type.
Unlike arrays, vectors can grow or shrink dynamically.
Creating a Vector
Syntax:
Example:
Output:
Explanation:
Vec<i32>creates a vector of integers.Vec::new()creates an empty vector.mutallows us to modify the vector.push()adds an element.
Creating a Vector Using vec!
Rust also provides the vec! macro.
Output:
Adding Elements Using push()
Output:
Removing Elements Using pop()
The pop() method removes and returns the last element.
Output:
Notice that pop() returns an Option<T> because the vector may be empty.
Accessing Vector Elements
There are two common ways to access vector elements.
Method 1: Using Index
Output:
Remember: Vector indexing starts from 0.
Index | Value |
|---|---|
0 | 10 |
1 | 20 |
2 | 30 |
Accessing an invalid index using [] causes a panic.
Method 2: Using get()
Output:
The get() method returns Option<&T>, so it allows you to handle an invalid index safely.
Updating Vector Elements
Output:
Finding the Length of a Vector
Output:
Iterating Through a Vector
You can use a for loop to access every element.
Output:
Here, &numbers borrows the vector, so the vector remains available after the loop.
Iterating and Updating Elements
Output:
The * dereferences the mutable reference so we can update the value.
3. Understanding HashMap (HashMap<K, V>)
A HashMap stores information as key-value pairs.
For example:
Key | Value |
|---|---|
Name | Hemant |
Course | Rust |
Age | 32 |
In a HashMap:
- Every key is unique.
- A key is used to access its corresponding value.
- Values can be of the same type.
Creating a HashMap
First, import HashMap:
Example:
The order of displayed entries is not guaranteed.
Accessing Values Using get()
Output:
get() returns an Option<&V> because the requested key may not exist.
Handling Missing Keys
Output:
Updating Values
You can insert a value using an existing key to replace its previous value.
Output:
Removing Elements
Use remove() to remove a key and its associated value.
Checking Whether a Key Exists
Use contains_key().
Output:
Iterating Through a HashMap
The order of entries is not guaranteed.
4. Understanding HashSet (HashSet<T>)
A HashSet is a collection that stores unique values.
It does not allow duplicate elements.
Example:
The set contains only:
The order is not guaranteed.
Notice that 10 was inserted twice, but the HashSet stores it only once.
Checking Whether a Value Exists
Use contains().
Output:
Removing Values
The set now contains 10 and 30.
Set Operations
Rust supports common mathematical set operations.
Consider two sets:
Meaning:
Operation | Result |
|---|---|
Union | All unique elements from both sets |
Intersection | Elements common to both sets |
Difference | Elements in the first set but not the second |
Expected values:
- Union: 1, 2, 3, 4, 5
- Intersection: 3
- Difference: 1, 2
The displayed order is not guaranteed.
5. Difference Between Vector, HashMap, and HashSet
Feature | Vector | HashMap | HashSet |
|---|---|---|---|
Stores | Values | Key-value pairs | Unique values |
Duplicates | Allowed | Keys are unique | Not allowed |
Access | Index | Key | Membership check |
Order | Insertion order | Not guaranteed | Not guaranteed |
Example | Student names | Student ID and name | Unique student IDs |
6. When Should You Use Each Collection?
Use Vector when:
- You need a list of student names.
- You want to store product prices.
- You need to maintain a sequence of values.
Use HashMap when:
- You want to store student ID and student name.
- You want to store product ID and price.
- You need to access information using a key.
Use HashSet when:
- You want to store unique email addresses.
- You want to avoid duplicate student IDs.
- You need to check whether an item already exists.