Learn About Collections in Rust

Introduction

In Rust programming, collections play a vital role in managing and manipulating data efficiently. Understanding how to work with collections effectively is essential for building robust and high-performance applications. In this article, we will explore the various collections and their features, functionalities, and best practices for utilization available in Rust. Collections are like different containers that hold and manage data. Think of them as toolboxes for keeping and managing different kinds of information. Following are the different types of collections that are used in the Rust.

Arrays

Arrays are a fundamental data structure that allows you to store a fixed-size sequence of elements of the same type. Unlike vectors, which can grow or shrink in size, arrays have a fixed length that cannot be changed after creation. Let's understand how to declare, access, and update an array by the following example.

let num: [i32; 5] = [4, 2, 3, 5, 9];

let num_first = num[0]; 
let num_third = num[2]; 

let mut mut_num = num; 
mut_num[1] = 10; 

println!("{:?}", mut_num); 

Output

array-output

Vectors

Vectors in Rust are like dynamic arrays that can grow or shrink in size as needed. They are a flexible and versatile collection type that allows you to store multiple elements of the same type. Vectors are stored on the heap, which means they can change in size during runtime without needing to know their size at compile time, unlike arrays. Let's explore the operations related to vectors by following example.

fn main() {

    let mut num: Vec<i32> = Vec::new();

    num.push(1);
    num.push(2);
    num.push(3);

    println!("First element: {}", num[0]); 

    for num in &num {
        println!("{}", num);
    }

    num[0] = 10;
    
    let removed_num = num.pop();
    println!("Removed element: {:?}", removed_num); 

    println!("Updated vector: {:?}", num); 
}

Output

vector-output

Slices

Slices in Rust are references to a contiguous sequence of elements in a collection. They provide a flexible and safe way to work with a portion of a collection without needing to copy or own the data. Slices are particularly useful when you want to operate on a subset of data within an array, vector, or string. Let's understand the working of slices by the following example.

fn main() {
    let mut numbers = [1, 2, 3, 4, 5];

    let slice = &numbers[1..4]; 

    println!("Slice: {:?}", slice); 

    let mut mut_numbers = [0; 3];
    mut_numbers.copy_from_slice(&numbers[1..4]);

    mut_numbers.copy_from_slice(&[10, 20, 30]); 

    for &num in slice {
        println!("Element: {}", num);
    }
}

Output

slice-output

Strings

Strings are a way to store and manipulate text. They come in two main types- 'String', which is mutable and growable, and '&str', which is a string slice and typically points to a portion of memory containing a string.

String - 'String' is a growable, mutable, heap-allocated string in Rust. You can create a new 'String' by using the 'String::new()' function and then adding text to it. Let's understand with the following example.

let mut string1 = String::new(); 
  string1.push_str("Hello, "); 
  string1.push('W'); 
  string1.push_str("orld!"); 
  println!("{}", string1); 

Output

string-output

&str - '&str' is a string slice, which is a reference to a sequence of UTF-8 bytes stored elsewhere. You can create a string slice from a string literal or from a 'String' using the '&' operator. Let's explore the following example.

let string_literal = "Hello, World!"; 
  let string_slice: &str = &string_literal; 
  println!("{}", string_slice); 
  
  let my_string = String::from("Hello Rustaceans"); 
  let string_slice: &str = &my_string; 
  println!("{}", string_slice);  

Output

string2-output

Hash Maps

A hash map is a collection type that stores key-value pairs. It's like a dictionary where you can look up a word (the key) and find its definition (the value). Hash maps are useful when you need to quickly find a value associated with a particular key, as they provide fast lookups. Let's understand with the following example.

use std::collections::HashMap;

fn main() {

    let mut scores = HashMap::new();

  
    scores.insert("Ankur", 100);
    scores.insert("Shekhar", 90);
    scores.insert("Gautam", 80);

    println!("Ankur's score: {}", scores["Ankur"]); 

    scores.insert("Ankur", 95); 

    if let Some(score) = scores.get("Shekhar") {
        println!("Shekhar's score: {}", score); 
    }

    scores.remove("Gautam"); 

    for (name, score) in &scores {
        println!("{} has a score of {}", name, score);
    }
}

Output

hashmap-output

  • We first create a new empty hash map called 'scores' using 'HashMap::new()'.
  • We then insert key-value pairs into the hash map using the 'insert' method.
  • We access values by key using square brackets ('[]'). If the key is not present, it will panic.
  • We can update a value by inserting a new value with the same key.
  • We can check if a key exists using the 'get' method, which returns an 'Option' containing a reference to the value.
  • We can remove a key-value pair using the 'remove' method.
  • We can iterate over key-value pairs using a 'for' loop with pattern matching.

Hash maps are flexible and efficient data structures for various applications, such as caching, indexing, and associative mapping. They are commonly used in Rust for storing and retrieving data based on keys.

Conclusion

Collections are essential tools in Rust programming for managing and manipulating data efficiently. Arrays provide a fixed-size sequence of elements, while vectors offer dynamic resizing capabilities. Slices allow working with portions of collections without copying data. Strings come in two types: mutable 'String' and immutable '&str'. Finally, hash maps store key-value pairs efficiently, making them useful for various applications like caching and indexing.

FAQ's

Q. Why are collections important in Rust?

A. Collections are crucial in Rust because they enable developers to organize and handle data effectively, leading to the development of robust and high-performance applications.

Q. What is the difference between arrays and vectors in Rust?

A. Arrays have a fixed size that cannot be changed after creation, while vectors are dynamic arrays that can grow or shrink in size as needed.

Q. What is a hash map, and why is it useful in Rust?

A. A hash map is a collection type that stores key-value pairs, providing fast lookups. It is useful for various applications like caching, indexing, and associative mapping in Rust.


Similar Articles