Rust-Programming-Language/C2/Collections/English
| Visual Cue | Narration |
| Slide 1 | Welcome to the Spoken Tutorial on Collections in Rust. |
| Slide 2
Learning Objectives |
In this tutorial, we will learn about:
|
| Slide 3
System Requirements |
|
| Slide 4
Code Files |
|
| Slide 6:
Collections |
Let us get started.
|
| Slide7:
Types of Collections |
Rust has 3 common collections that are used very often.
|
| Open Visual code editor | Open the Visual Studio code editor. |
| In the menu bar, click on Terminal and select New Terminal. | In the menu bar, click on Terminal and select New Terminal. |
| > cd Desktop/MyRustProject
> cargo new collections In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> collections |
Let us go to our working directory MyRustProject as explained earlier.
Type the command cargo new collections and press Enter. Open the created project as shown. |
| Point to the main.rs file. | In the main.rs file, copy and paste the code from the Code file. |
| fn main() {
let mut fruits = vec!["Apple"]; fruits.push("Banana"); fruits.push("Cherry"); // Accessing elements safely println!("{:?}", fruits.get(1)); // Some("Banana") println!("{:?}", fruits.get(5)); // None // Removing last element fruits.pop(); println!("{:?}", fruits); // ["Apple", "Banana"] // Checking if empty println!("{}", fruits.is_empty()); // Checking length println!("Number of fruits: {}", fruits.len()); } |
This code explains how the vector works.
We declare a mutable vector called fruits and it is initialized with "Apple". push() is a method to add elements to the end of the vector. We add banana and cherry to the second and third position using the push method. We use the get() method which returns an Option enum. fruits.get(1) accesses the element at index 1 and prints Some(“Banana”) fruits.get(5) is an invalid index and so it prints None. .pop() method removes the last element from the vector. In our case, "Cherry" is the last item and it gets removed..is_empty() checks if the vector has zero elements..len() returns the number of elements present in the vector.Save the program. |
| Run the code to see the output.
is_empty() returns false since the vector is not empty. The vector has 2 elements and so the len method prints, Number of fruits as 2. | |
| Next we will see about HashMap. | |
| use std::collections::HashMap;
fn main() { let mut capitals = HashMap::new(); // Insert key-value pairs capitals.insert("India", "New Delhi"); capitals.insert("USA", "Washington D.C."); // Accessing elements println!("{:?}", capitals.get("India")); // Some("New Delhi") println!("{:?}", capitals.get("France")); // None // Removing an entry capitals.remove("USA"); // Checking if key exists if capitals.contains_key("India") { println!("Found India's capital!"); } |
Copy paste the code from the Code file.
In Rust, HashMap is not available by default.We need to import it from the std::collections module to use it. We create a new, empty, and mutable HashMap called capitals. insert() method allows us to store a key with its value. The keys are the country names and the values are their capitals Similar to Vector, we use the .get() method, to retrieve the value for a given key. If the key exists, it returns Some(value). If the key does not exist, it returns None. The remove() method deletes the entry with the given key. It removes "USA" and its capital "Washington D.C." from the HashMap. The contains_key() method checks if a specific key is present inside the HashMap. The len() method gives the total number of key-value pairs present inside the HashMap. Save the program. |
| Run the program to see the output. | |
| Next we will see about Hashset. | |
| Slide:
Hashset: |
|
| Switch back to the visual code editor.
Copy paste the code from the Code file. | |
| use std::collections::HashSet;
fn main() { let mut names = HashSet::new(); // Inserting elements names.insert("Jack"); names.insert("Joe"); names.insert("Jill"); names.insert("Joe"); // Duplicate, will be ignored if names.contains("Joe") { println!("Joe is present!"); } // Removing an element names.remove("Jill"); // Checking length println!("Number of persons: {}", names.len()); |
In this code, we create a mutable empty HashSet, called names.
.insert() method insert elements to the HashSet. When we try to insert the name Joe twice, the second insertion is ignored automatically.
The .contains() method checks if a specific item is present inside the HashSet. It will print a success message if the name joe exists. The remove() method deletes a specific value from the set. We can also loop through all the elements and print them. The order is not guaranteed because HashSets are unordered. Save the program. |
| In the terminal, type cargo run and see the output. | |
| This brings us to the end of this tutorial.
Let us summarize. | |
| Slide 8 | As an assignment,
Write a Rust program that:
|
| Slide 9
Thank You |
Thanks for joining. |