Rust101: Collections

Mukundh Bhushan
5 min readApr 24, 2020

--

In the previous article, we discussed about Mutability in rust. Now let’s dive into Collections.

TL;DR

Types of collections in Rust

  • Arrays
  • Tuples
  • Hashmaps
  • Vectors

First, let’s start off by creating a new project called “Collections” using the following command

cargo new Collections --bin

Type of Collections in Rust

  • Arrays
  • Tuples
  • Hashmaps
  • Vectors

Arrays

Let's start off with arrays.

Arrays in Rust are fixed length collections in which elements of similar datatype only can be stored.

Here is the syntax to define an array in Rust

let <var name> = [<values>]; //implicit
let <var name>:[<data type>; <array length>] = [<values>] //explicit

Assessing elements in an array

Reassigning value to a mutable array

Other operations with arrays

Length of an array

<array>.len()

Get stack allocated for an array

mem::size_of_val(&<array name>)

Slice of an array

let <array1 name>: &[<data type>] = &<main array name>[<start index>..<end index>];

List of all the above operations

Tuples

Arrays in rust are collections of items which are of the same datatype. Tuples on the other hand allow for elements of different datatypes to be part of a single collection.

Here is the syntax to define a tuple in Rust

//explicit
let <tuple name>: (<datatype1>,<data type2>) = (<values>); //implicit
let <tuple name> = (<values>);

Assessing elements

Tuples use the “.” operator

Tuples in Rust allow for another method to assess elements

The complier might throw a couple of warnings because of the variable names.

HashMaps

HashMap are the key-value pair-based collections in rust.
HashMap’s allows for insertion and deletion of values in it.

The standard collection package must be imported to use it.

use std::collections::HashMap

Here is the syntax to define a HashMap in Rust

let mut marks = HashMap::new();

Adding values

the “.insert()” method is used

<hashmap name>.insert("key","value");

Deleting an element from an HashMap

The “.remove()” method is used

<hashmap name>.remove(<key>);

Looping through the HashMap

Checking if a key exists

The “.contains_key()” method is used. This method returns a Boolean

Finding length of a HashMap

the “.len()” method is used

<Hashmap name>.len()

Assessing value using a given key

Unlike arrays and tuple which use the elements position as HashMap's use are stored as key-value pairs. Rust searches with respect to the key provided therefore the “match.get()” function is used.

This function is has two parts

  • Some() → when the given key is found in the HashMap.
  • None → if the given key is not part of the HashMap

List of all the above operations

Vectors

Vectors in Rust are resizable arrays. There is no need to import any library to use it.

Here is the syntax to define a HashMap in Rust

let mut <vector name>: Vec<<datatype>> = vec![<values>]

Assessing elements in a vector

Reassigning values to a vector

The vector declared must be of type “mut” to reassign values to the vector

Adding elements to a vector

Removing elements from a vector

There are two methods:-

  • pop →to remove last element
  • remove → delete element at a specific index

finding length of an array

<vector name>.len();

Finding stack allocated

mem::size_of_val(&numbers)

Slice a vector

let <slice name>: &[<datatype>] = &<vector name>[<start index>..<end index>];

Looping through values in a vector

The “.iter()” method is used

for x in numbers.iter() {println!("Number: {}", x);}

Loop & mutate values

The “iter_mut()” method is used

for x in numbers.iter_mut() {
*x *= 2;
//or *x = *x*2;}

List of all the above operations

In the next article, we will discuss about mutability in variables.

link to the next article

Link to the previous article

--

--