Difference between revisions of "Rust-Programming-Language/C2/Collections/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000...")
 
Line 5: Line 5:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Collections in Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Collections in Rust.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 2'''</div>
+
|| '''Slide 2'''
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
'''Learning Objectives'''
 
|| In this tutorial, we will learn about:
 
|| In this tutorial, we will learn about:
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Collections an</span>d</div>
+
* Collections and
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Types of Collections</div>
+
* Types of Collections
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 3'''</div>
+
|| '''Slide 3'''
  
<span style="color:#000000;">'''System Requirements'''</span>
+
'''System Requirements'''
 
||
 
||
 
* To record this tutorial I’m using the following setup.
 
* To record this tutorial I’m using the following setup.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''4'''</span>
+
|| '''Slide''' '''4'''
  
<div style="color:#000000;">'''Code Files'''</div>
+
'''Code Files'''
 
||
 
||
* <span style="color:#000000;">The following code file is required to practise this tutorial.</span>
+
* The following code file is required to practise this tutorial.
* <span style="color:#000000;">This file is provided in the </span><span style="color:#000000;">'''Code Files'''</span><span style="color:#000000;"> link of this tutorial page.</span>
+
* This file is provided in the '''Code Files''' link of this tutorial page.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 6:'''</div>
+
|| '''Slide 6:'''
  
<div style="color:#000000;">'''Collections'''</div>
+
'''Collections'''
 
|| Let us get started.
 
|| Let us get started.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Collections'''</span><span style="color:#000000;"> are data structures that hold multiple values together</span></div>
+
* '''Collections''' are data structures that hold multiple values together
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">They are used to store, retrieve, and manipulate groups of related values efficiently.</div>
+
* They are used to store, retrieve, and manipulate groups of related values efficiently.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Rust's collection types </span>are available<span style="color:#000000;"> in the </span><span style="color:#000000;">'''std::collections'''</span><span style="color:#000000;"> module </span></div>
+
* Rust's collection types are available in the '''std::collections''' module  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide7:'''</div>
+
|| '''Slide7:'''
  
<span style="color:#000000;">'''Types of Collections'''</span>
+
'''Types of Collections'''
|| <div style="color:#000000;">Rust has 3 common collections that are used very often.</div>
+
|| Rust has 3 common collections that are used very often.
  
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Vectors'''</span> <span style="background-color:#ffffff;color:#171717;">are dynamic arrays that can grow or shrink in size as needed.</span></div>
+
* '''Vectors''' are dynamic arrays that can grow or shrink in size as needed.
  
<div style="color:#000000;"></div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Hash Map'''</span> is a <span style="color:#000000;">key-value pair storage for fast lookups by key</span></div>
 
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''HashSet'''</span> is a <span style="color:#000000;">collection of unique items with</span> no<span style="color:#000000;"> duplicates</span></div>
 
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
* '''Hash Map''' is a key-value pair storage for fast lookups by key
|| <span style="color:#000000;">Open </span><span style="color:#000000;">'''Visual'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''code editor'''</span>
+
* '''HashSet''' is a collection of unique items with no duplicates
|| <span style="color:#000000;">Open the </span><span style="color:#000000;">'''Visual </span><span style="color:#000000;">S</span><span style="color:#000000;">tudio code editor.'''</span>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''Terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span><span style="color:#000000;">.</span>
+
  
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''Terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span><span style="color:#000000;">.</span>
+
|-
 +
|| Open '''Visual''' '''code editor'''
 +
|| Open the '''Visual Studio code editor.'''
 +
|-
 +
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
|| <span style="color:#000000;">'''> cd'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''Desktop'''</span><span style="color:#000000;">/</span><span style="color:#000000;">'''MyRustProject'''</span>
+
  
<span style="color:#000000;">'''>'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''cargo new collections'''</span>
+
|-
 +
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
  
<div style="color:#000000;">'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> collections'''</div>
+
'''>''' '''cargo new collections'''
|| <span style="color:#000000;">Let us go to our working directory </span><span style="color:#000000;">'''MyRustProject'''</span><span style="color:#000000;"> as explained earlier.</span>
+
  
<span style="color:#000000;">Type the command </span><span style="color:#000000;">'''cargo new collections '''</span><span style="color:#000000;">and press </span><span style="color:#000000;">'''Enter.'''</span>
+
'''In the menu bar, File >> Open folder >>  
  
<div style="color:#000000;">Open the created project as shown.</div>
+
Desktop >> MyRustProject >> collections'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
|| <span style="color:#000000;">Point to the </span><span style="color:#000000;">'''main.rs'''</span><span style="color:#000000;"> file.</span>
+
  
|| <span style="background-color:#ffffff;color:#000000;">In the </span><span style="background-color:#ffffff;color:#000000;">'''main.rs '''</span><span style="background-color:#ffffff;color:#000000;">file, copy and paste the code from the Code file.</span>
+
Type the command '''cargo new collections '''and press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">fn main() {</div>
+
  
<div style="color:#000000;">let mut fruits = vec!["Apple"];</div>
+
Open the created project as shown.
 +
|-
 +
|| Point to the '''main.rs''' file.
  
<div style="color:#000000;">fruits.push("Banana");</div>
+
|| In the '''main.rs '''file, copy and paste the code from the Code file.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;">fruits.push("Cherry");</div>
+
let mut fruits = vec!["Apple"];
  
<div style="color:#000000;">// Accessing elements safely</div>
+
fruits.push("Banana");
  
<div style="color:#000000;">println!("{:?}", fruits.get(1)); // Some("Banana")</div>
+
fruits.push("Cherry");
  
<div style="color:#000000;">println!("{:?}", fruits.get(5)); // None</div>
+
// Accessing elements safely
  
<div style="color:#000000;">// Removing last element</div>
+
println!("{:?}", fruits.get(1)); // Some("Banana")
  
<div style="color:#000000;">fruits.pop();</div>
+
println!("{:?}", fruits.get(5)); // None
  
<div style="color:#000000;">println!("{:?}", fruits); // ["Apple", "Banana"]</div>
+
// Removing last element
  
<div style="color:#000000;">// Checking if empty</div>
+
fruits.pop();
  
<div style="color:#000000;">println!("{}", fruits.is_empty());</div>
+
println!("{:?}", fruits); // ["Apple", "Banana"]
  
<div style="color:#000000;">// Checking length</div>
+
// Checking if empty
  
<div style="color:#000000;">println!("Number of fruits: {}", fruits.len());</div>
+
println!("{}", fruits.is_empty());
  
<div style="color:#000000;">}</div>
+
// Checking length
|| <div style="color:#000000;">This code explains how the vector works.</div>
+
  
<span style="color:#252525;">We declare a </span><span style="color:#252525;">'''mutable vector'''</span><span style="color:#252525;"> called </span><span style="color:#252525;">'''fruits'''</span><span style="color:#252525;"> and i</span><span style="color:#252525;">t is initialized with </span><span style="color:#188038;">"Apple"</span><span style="color:#252525;">.</span>
+
println!("Number of fruits: {}", fruits.len());
  
<span style="color:#188038;">push()</span><span style="color:#252525;"> </span><span style="color:#252525;">is a </span><span style="color:#252525;">method to add elements</span><span style="color:#252525;"> </span><span style="color:#252525;">to the end of the vector.</span>
+
}
 +
|| This code explains how the vector works.
  
<span style="color:#252525;">We add </span><span style="color:#252525;">'''banana'''</span><span style="color:#252525;"> and </span><span style="color:#252525;">'''cherry'''</span><span style="color:#252525;"> to the second and third position using the </span><span style="color:#252525;">'''push'''</span><span style="color:#252525;"> method.</span>
+
We declare a '''mutable vector''' called '''fruits''' and it is initialized with "Apple".
  
W<span style="color:#252525;">e use the </span><span style="color:#252525;">'''get() '''</span><span style="color:#252525;">method which returns an </span><span style="color:#252525;">'''Option enum'''</span><span style="color:#252525;">.</span>
+
push() is a method to add elements to the end of the vector.
  
<span style="color:#252525;">'''fruits.get(1) '''</span><span style="color:#252525;">accesses the element at index 1 and prints </span><span style="color:#252525;">'''Some(“Banana”)'''</span>
+
We add '''banana''' and '''cherry''' to the second and third position using the '''push''' method.
  
<span style="color:#252525;">'''fruits.get(5) '''</span><span style="color:#252525;">is an invalid index and so it prints </span><span style="color:#252525;">'''None.'''</span>
+
We use the '''get() '''method which returns an '''Option enum'''.
  
<span style="color:#188038;">.pop()</span><span style="color:#252525;"> method</span><span style="color:#252525;"> removes the last element </span><span style="color:#252525;">from the vector.</span>
+
'''fruits.get(1) '''accesses the element at index 1 and prints '''Some(“Banana”)'''
  
<span style="color:#252525;">In our case, </span><span style="color:#188038;">"Cherry"</span><span style="color:#000000;"> is </span><span style="color:#252525;">the last item and it gets removed.</span><span style="color:#188038;">.is_empty()</span><span style="color:#252525;"> </span><span style="color:#252525;">checks if the vector has </span><span style="color:#252525;">zero elements.</span><span style="color:#188038;">.len()</span><span style="color:#252525;"> returns the </span><span style="color:#252525;">number of elements </span><span style="color:#252525;">present in the vector.</span><span style="background-color:#ffffff;color:#000000;">Save the program.</span>
+
'''fruits.get(5) '''is an invalid index and so it prints '''None.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
 +
.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.
 
|| Run the code to see the output.
  
<span style="color:#188038;">is_empty()</span><span style="color:#252525;"> returns false since the </span><span style="color:#252525;">vector</span><span style="color:#252525;"> is not empty.</span>
+
is_empty() returns false since the vector is not empty.
  
<span style="color:#252525;">The vector has 2 elements and so the </span><span style="color:#252525;">'''len'''</span><span style="color:#252525;"> method prints,</span>
+
The vector has 2 elements and so the '''len''' method prints,
  
<span style="color:#188038;">Number of fruits as 2</span><span style="color:#252525;">.</span>
+
Number of fruits as 2.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#252525;" |  
+
||  
|| <span style="color:#252525;">Next we will see about </span><span style="color:#252525;">'''HashMap.'''</span>
+
|| Next we will see about '''HashMap.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#252525;">use std::collections::HashMap;</div>
+
|| use std::collections::HashMap;
  
<div style="color:#252525;">fn main() {</div>
+
fn main() {
  
<div style="color:#252525;">let mut capitals = HashMap::new();</div>
+
let mut capitals = HashMap::new();
  
<div style="color:#252525;">// Insert key-value pairs</div>
+
// Insert key-value pairs
  
<div style="color:#252525;">capitals.insert("India", "New Delhi");</div>
+
capitals.insert("India", "New Delhi");
  
<div style="color:#252525;">capitals.insert("USA", "Washington D.C.");</div>
+
capitals.insert("USA", "Washington D.C.");
  
<div style="color:#252525;">// Accessing elements</div>
+
// Accessing elements
  
<div style="color:#252525;">println!("{:?}", capitals.get("India"));&nbsp; // Some("New Delhi")</div>
+
println!("{:?}", capitals.get("India"));&nbsp; // Some("New Delhi")
  
<div style="color:#252525;">println!("{:?}", capitals.get("France")); // None</div>
+
println!("{:?}", capitals.get("France")); // None
  
<div style="color:#252525;">// Removing an entry</div>
+
// Removing an entry
  
<div style="color:#252525;">capitals.remove("USA");</div>
+
capitals.remove("USA");
  
<div style="color:#252525;">// Checking if key exists</div>
+
// Checking if key exists
  
<div style="color:#252525;">if capitals.contains_key("India") {</div>
+
if capitals.contains_key("India") {
  
<div style="color:#252525;">println!("Found India's capital!");</div>
+
println!("Found India's capital!");
  
<div style="color:#252525;">}</div>
+
}
 
|| Copy paste the code from the Code file.
 
|| Copy paste the code from the Code file.
  
<span style="color:#252525;">In </span><span style="color:#252525;">Rust,</span><span style="color:#252525;"> </span><span style="color:#252525;">'''HashMap'''</span><span style="color:#252525;"> </span><span style="color:#252525;">is not available by default.</span><span style="color:#252525;">We need to </span><span style="color:#252525;">'''import '''</span><span style="color:#252525;">it</span><span style="color:#252525;"> </span><span style="color:#252525;">from the </span><span style="color:#188038;">std::collections</span><span style="color:#252525;"> </span><span style="color:#252525;">module to use it.</span>
+
In Rust, '''HashMap''' is not available by default.We need to '''import '''it from the std::collections module to use it.
  
<span style="color:#000000;">We create a </span><span style="color:#000000;">'''new, empty'''</span><span style="color:#000000;">,</span><span style="color:#000000;"> and </span><span style="color:#000000;">'''mutable'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''HashMap'''</span><span style="color:#000000;"> called </span><span style="color:#188038;">'''capitals'''</span><span style="color:#000000;">.</span>
+
We create a '''new, empty''', and '''mutable''' '''HashMap''' called '''capitals'''.
  
<span style="color:#188038;">insert()</span><span style="color:#000000;"> method allows us to store a </span><span style="color:#000000;">'''key'''</span><span style="color:#000000;"> with its </span><span style="color:#000000;">value.</span>
+
insert() method allows us to store a '''key''' with its value.
  
 
The keys are the country names and the values are their capitals
 
The keys are the country names and the values are their capitals
  
<span style="color:#000000;">Similar to Vector, we </span>use the<span style="color:#000000;"> </span><span style="color:#000000;">'''.get()'''</span><span style="color:#000000;"> method, </span>to retrieve<span style="color:#000000;"> the value for</span> a<span style="color:#000000;"> given key.</span>
+
Similar to Vector, we use the '''.get()''' method, to retrieve the value for a given key.
  
<span style="color:#000000;">If the key exists, it returns </span><span style="color:#188038;">Some(value)</span><span style="color:#000000;">.</span>
+
If the key exists, it returns Some(value).
  
<span style="color:#000000;">If the key does not exist, it returns </span><span style="color:#188038;">None</span><span style="color:#000000;">.</span>
+
If the key does not exist, it returns None.
  
<span style="color:#000000;">The </span><span style="color:#188038;">remove()</span><span style="color:#000000;"> method </span><span style="color:#000000;">deletes </span><span style="color:#000000;">the entry with the given key.</span>
+
The remove() method deletes the entry with the given key.
  
<span style="color:#252525;">It removes</span><span style="color:#000000;"> </span><span style="color:#188038;">"USA"</span><span style="color:#000000;"> and its capital </span><span style="color:#188038;">"Washington D.C."</span><span style="color:#000000;"> from the</span><span style="color:#000000;">''' HashMap.'''</span>
+
It removes "USA" and its capital "Washington D.C." from the''' HashMap.'''
  
<span style="color:#000000;">The </span><span style="color:#188038;">contains_key()</span><span style="color:#000000;"> method checks if a </span><span style="color:#000000;">specific</span><span style="color:#000000;">''' key'''</span><span style="color:#000000;"> is present inside the</span><span style="color:#000000;">''' HashMap'''</span><span style="color:#000000;">.</span>
+
The contains_key() method checks if a specific''' key''' is present inside the''' HashMap'''.
  
<span style="color:#252525;">The </span><span style="color:#188038;">len()</span><span style="color:#252525;"> method gives the</span><span style="color:#252525;"> total number of</span><span style="color:#252525;">''' key-value pairs'''</span><span style="color:#252525;"> present inside the </span><span style="color:#252525;">'''HashMap'''</span><span style="color:#252525;">.</span>
+
The len() method gives the total number of''' key-value pairs''' present inside the '''HashMap'''.
  
<div style="color:#000000;">Save the program.</div>
+
Save the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
 
|| Run the program to see the output.
 
|| Run the program to see the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <span style="color:#000000;">Next we will see about </span><span style="color:#000000;">'''Hashset'''</span><span style="color:#000000;">.</span>
+
|| Next we will see about '''Hashset'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">Slide:</div>
+
|| Slide:
  
<div style="color:#000000;">Hashset:</div>
+
Hashset:
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">It</span><span style="color:#252525;"> is a</span><span style="color:#252525;"> collection of unique values.</span></div>
+
* It is a collection of unique values.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">It does not store duplicate entries, </span><span style="color:#252525;">that is</span><span style="color:#252525;"> </span><span style="color:#252525;">n</span><span style="color:#252525;">o two identical items can exist</span><span style="color:#252525;"> in it.</span></div>
+
* It does not store duplicate entries, that is no two identical items can exist in it.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#252525;">It is unordered — </span><span style="color:#252525;">T</span><span style="color:#252525;">he elements do not have a guaranteed order inside the set.</span></div>
+
* It is unordered — The elements do not have a guaranteed order inside the set.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <div style="color:#252525;">Switch back to the visual code editor.</div>
+
|| Switch back to the visual code editor.
  
 
Copy paste the code from the Code file.
 
Copy paste the code from the Code file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#252525;">use std::collections::HashSet;</div>
+
|| use std::collections::HashSet;
  
<div style="color:#252525;">fn main() {</div>
+
fn main() {
  
<div style="color:#252525;">let mut names = HashSet::new();</div>
+
let mut names = HashSet::new();
  
<div style="color:#252525;">// Inserting elements</div>
+
// Inserting elements
  
<div style="color:#252525;">names.insert("Jack");</div>
+
names.insert("Jack");
  
<div style="color:#252525;">names.insert("Joe");</div>
+
names.insert("Joe");
  
<div style="color:#252525;">names.insert("Jill");</div>
+
names.insert("Jill");
  
<div style="color:#252525;">names.insert("Joe"); // Duplicate, will be ignored</div>
+
names.insert("Joe"); // Duplicate, will be ignored
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;if names.contains("Joe") {</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;if names.contains("Joe") {
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;println!("Joe is present!");</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;println!("Joe is present!");
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;}</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;}
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;// Removing an element</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;// Removing an element
  
<div style="color:#252525;">names.remove("Jill");</div>
+
names.remove("Jill");
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;// Checking length</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;// Checking length
  
<div style="color:#252525;">&nbsp;&nbsp;&nbsp;&nbsp;println!("Number of persons: {}", names.len());</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;println!("Number of persons: {}", names.len());
|| <span style="color:#252525;">In this code, we create a </span><span style="color:#252525;">'''mutable'''</span><span style="color:#252525;"> empty </span><span style="color:#188038;">HashSet</span><span style="color:#252525;">, called </span><span style="color:#188038;">'''names.'''</span>
+
|| In this code, we create a '''mutable''' empty HashSet, called '''names.'''
  
<span style="color:#252525;">'''.insert()'''</span><span style="color:#252525;"> method insert elements to the </span><span style="color:#252525;">'''HashSet'''</span><span style="color:#252525;">.</span>
+
'''.insert()''' method insert elements to the '''HashSet'''.
  
<span style="color:#252525;">When we try to insert the name</span><span style="color:#252525;"> </span><span style="color:#252525;">'''Joe'''</span><span style="color:#252525;"> </span><span style="color:#252525;">twice</span><span style="color:#252525;">,</span><span style="color:#252525;"> the </span><span style="color:#252525;">second insertion is </span><span style="color:#252525;">ignored</span><span style="color:#252525;"> </span><span style="color:#252525;">automatically</span><span style="color:#252525;">.</span>
+
When we try to insert the name '''Joe''' twice, the second insertion is ignored automatically.
  
<div style="color:#252525;"></div>
 
  
<span style="color:#252525;">Hash sets only store </span><span style="color:#252525;">unique </span><span style="color:#252525;">items.</span>
+
Hash sets only store unique items.
  
<span style="color:#000000;">The .</span><span style="color:#188038;">contains()</span><span style="color:#000000;"> method checks if a</span><span style="color:#000000;"> specific </span><span style="color:#000000;">'''item '''</span><span style="color:#000000;">is present inside the </span><span style="color:#000000;">'''HashSet'''</span><span style="color:#000000;">.</span>
+
The .contains() method checks if a specific '''item '''is present inside the '''HashSet'''.
  
<span style="color:#000000;">It will print a success message if the name </span><span style="color:#000000;">'''joe'''</span><span style="color:#000000;"> exists.</span>
+
It will print a success message if the name '''joe''' exists.
  
<span style="color:#252525;">The </span><span style="color:#252525;">'''remove()'''</span><span style="color:#252525;"> method deletes a specific value from the set. </span>
+
The '''remove()''' method deletes a specific value from the set.  
  
<span style="color:#252525;">W</span><span style="color:#252525;">e can also loop through all the elements and print them.</span>
+
We can also loop through all the elements and print them.
  
<span style="color:#252525;">The order is not guaranteed because </span><span style="color:#252525;">'''HashSets'''</span><span style="color:#252525;"> are unordered.</span>
+
The order is not guaranteed because '''HashSets''' are unordered.
  
<div style="color:#252525;">Save the program.</div>
+
Save the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run '''</span><span style="color:#000000;">and see the output.</span>
+
|| In the terminal, type '''cargo run '''and see the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
|| This brings us to the end of this tutorial.
  
<div style="color:#000000;">Let us summarize. </div>
+
Let us summarize.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 8'''</div>
+
|| '''Slide 8'''
  
 
|| As an assignment,
 
|| As an assignment,
  
<div style="color:#000000;">Write a Rust program that:</div>
+
Write a Rust program that:
# <div style="margin-left:1.247cm;margin-right:0cm;">Takes<span style="color:#000000;"> a list of numbers </span></div>
+
# Takes a list of numbers  
# <div style="margin-left:1.247cm;margin-right:0cm;"><span style="color:#000000;">Uses a HashSet to find all the </span><span style="color:#000000;">'''unique numbers'''</span><span style="color:#000000;">.</span></div>
+
# Uses a HashSet to find all the '''unique numbers'''.
 +
 
 +
|-
 +
|| '''Slide 9 '''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
'''Thank You'''
| style="color:#000000;" | '''Slide 9 Thank You'''
+
|| Thanks for joining.
| style="color:#000000;" | Thanks for joining.
+
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Revision as of 18:08, 7 October 2025

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:
  • Collections and
  • Types of Collections
Slide 3

System Requirements

  • To record this tutorial I’m using the following setup.
Slide 4

Code Files

  • The following code file is required to practise this tutorial.
  • This file is provided in the Code Files link of this tutorial page.
Slide 6:

Collections

Let us get started.
  • Collections are data structures that hold multiple values together
  • They are used to store, retrieve, and manipulate groups of related values efficiently.
  • Rust's collection types are available in the std::collections module
Slide7:

Types of Collections

Rust has 3 common collections that are used very often.
  • Vectors are dynamic arrays that can grow or shrink in size as needed.


  • Hash Map is a key-value pair storage for fast lookups by key
  • HashSet is a collection of unique items with no duplicates
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:

  • It is a collection of unique values.
  • It does not store duplicate entries, that is no two identical items can exist in it.
  • It is unordered — The elements do not have a guaranteed order inside the set.
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.


Hash sets only store unique items.

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:

  1. Takes a list of numbers
  2. Uses a HashSet to find all the unique numbers.
Slide 9

Thank You

Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat