Sage-Code Laboratory
index<--

Rust Collections

In computer science, a data collection is a group of related data items that are stored and processed together as a unit. Data collections are also commonly referred as data structures.

Page Bookmarks


Elements

Data collections are a fundamental concept in computer science that provide a way to organize and manage data in a structured manner. Rust provides several built-in collections, each with its own strengths and weaknesses.

Before study different collections, we must learn about collection elements. In Rust collection elements can be empty. This feature require special attention.

Wrapping

In Rust, you have to unwrap elements of a collection because they are wrapped in Option types. Option is a type that can either contain a value or be empty. This is useful for representing situations where a value might not be present, such as when reading data from a file that might not exist.

Unwrapping

When you unwrap an Option, you are forcing it to contain a value. If the Option is empty, this will cause a panic. This is because Rust wants to make sure that you are aware of the possibility that the value might not be present.

Examples

There are a few ways to avoid having to unwrap Option types.

1. How to unwrap an Option using the match statement

One way is to use the match statement. The match statement allows you to check the value of an Option and take different actions depending on whether it is empty or not.

let my_option: Option = Some(42);

match my_option {
  Some(value) => println!("The value is {}", value),
  None => println!("The value is not present"),
}

This code will print the following output:

The value is 42

2. How to unwrap an Option using the ? operator

Another way to avoid having to unwrap Option types is to use the ? operator. The ? operator allows you to handle errors that might occur when trying to access the value of an Option.

let my_option: Option = Some(42);

let value = my_option?;

println!("The value is {}", value);

This code will also print the following output:

The value is 42

3. How to use the if let statement to unwrap an Option

Finally, you can also use the if let statement to check the value of an Option and take different actions depending on whether it is empty or not.

let my_option: Option = Some(42);

if let Some(value) = my_option {
  println!("The value is {}", value);
}

This code will also print the following output:

The value is 42

Vectors

Vectors, represented by the Vec type, are a dynamic array that can grow or shrink in size. Here's an example:


// Declare a vector of integers
let mut numbers = Vec::new();

// Add elements to the vector
numbers.push(10);
numbers.push(20);
numbers.push(30);

// Accessing elements in a vector
let second = numbers[1];

// Removing elements from a vector
let last = numbers.pop().unwrap();

The strength of vectors is their flexibility - they can hold any type of data and they can be resized at runtime. The weakness is that accessing individual elements can be slow, since they are not guaranteed to be adjacent in memory.

Arrays

Arrays, represented by the [T; N] type, are a fixed-size collection of elements of the same type. Here's an example:


// Declare an array of integers with length 3
let array = [1, 2, 3];

// Accessing elements in an array
let first = array[0];
let second = array[1];

// Iterating over elements in an array
for number in array.iter() {
    println!("{}", number);
}

The strength of arrays is their efficiency - they are guaranteed to be adjacent in memory and accessing individual elements is fast. The weakness is their inflexibility - they cannot be resized at runtime.

Hash Maps

Hash maps, represented by the HashMap type, are a collection of key-value pairs where each key is unique. Here's an example:


use std::collections::HashMap;

// Declare a hash map with string keys and integer values
let mut scores = HashMap::new();

// Insert values into the hash map
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

// Looking up values in a hash map
let blue_score = scores.get("Blue");

// Removing values from a hash map
scores.remove("Yellow");

The strength of hash maps is their fast access time for lookups and inserts, even with a large number of key-value pairs. The weakness is their lack of ordering - the order in which elements were inserted is not guaranteed.

Data Sets

Sets, represented by the HashSet type, are a collection of unique elements. Here's an example:


use std::collections::HashSet;

// Declare a set of integers
let mut numbers = HashSet::new();

// Add elements to the set
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);

// Checking if an element is in a set
let contains_10 = numbers.contains(&10);

// Removing elements from a set
numbers.remove(&30);

The strength of sets is their ability to efficiently check if an element is present or not, and their ability to efficiently remove elements. The weakness is their lack of ordering - the order in which elements were inserted is not guaranteed.


Read next: Control