Difference between revisions of "Rust-Programming-Language/C2/Arrays/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:#00000...")
 
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
  
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Arrays in Rust.'''</span>
 
|- 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>
 
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
'''Title Slide'''
 +
|| Welcome to the Spoken Tutorial on '''Arrays in Rust.'''
 +
|-
 +
|| '''Slide 2'''
 +
 
 +
'''Learning Objectives'''
 
|| In this tutorial, we will learn about:
 
|| In this tutorial, we will learn about:
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Arrays</div>
+
* Arrays
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-
|| <div style="color:#000000;">'''Slide 3'''</div>
+
|| '''Slide 3'''
  
<div style="color:#000000;">'''System Requirements'''</div>
+
'''System Requirements'''
 
|| To record this tutorial I am using:
 
|| To record this tutorial I am using:
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Ubuntu Linux OS version 22.04</div>
+
* Ubuntu Linux OS version 22.04
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Rust version 1.80.0 </div>
+
* Rust version 1.80.0  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Visual Studio Code version 1.19.0</div>
+
* Visual Studio Code version 1.19.0
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-
|| <div style="color:#000000;">'''Slide 4'''</div>
+
|| '''Slide 4'''
  
<div style="color:#000000;">'''Prerequisites'''</div>
+
'''Prerequisites'''
| style="color:#000000;" | To follow this tutorial, you should be familiar with compiling and running Rust programs.
+
|| To follow this tutorial, you should be familiar with compiling and running Rust programs.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-
|| <span style="color:#000000;">'''Slide'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''5'''</span>
+
|| '''Slide''' '''5'''
 +
 
 +
'''Code Files'''
  
<div style="color:#000000;">'''Code Files'''</div>
 
||
 
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">The following code file is required to practise this tutorial.</div>
 
 
*'''arraysdemo.rs'''
 
*'''arraysdemo.rs'''
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">This file is provided in the '''Code Files''' link of this tutorial page.</div>
+
||
 +
* The following code file is required to practise this tutorial.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
* This file is provided in the '''Code Files''' link of this tutorial page.
|| <div style="color:#000000;">'''Slide 6'''</div>
+
  
<div style="color:#000000;">'''Arrays'''</div>
+
|-
 +
|| '''Slide 6'''
 +
 
 +
'''Arrays'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">'''Arrays''' in '''Rust''' are collections of values of the same data type and have a fixed size</div>
+
* '''Arrays''' in '''Rust''' are collections of values of the same data type and have a fixed size
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Arrays once initialized cannot be resized</div>
+
* Arrays once initialized cannot be resized
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">An array declaration allocates sequential memory blocks</div>
+
* An array declaration allocates sequential memory blocks
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||  
|| <span style="color:#000000;">Let us understand how arrays </span>work<span style="color:#000000;"> in a</span><span style="color:#000000;">''' </span>R<span style="color:#000000;">ust'''</span><span style="color:#000000;"> program.</span>
+
|| Let us understand how arrays work in a''' Rust''' program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">Open </span>'''Visual Studio Code editor.'''
+
|| Open '''Visual Studio Code editor.'''
  
|| <span style="color:#000000;">Open the </span><span style="color:#000000;">'''Visual </span>S<span style="color:#000000;">tudio </span>C<span style="color:#000000;">ode editor.'''</span>
+
|| Open the '''Visual Studio Code editor.'''
|- 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>
+
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
|| <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>
+
|| 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;"
+
|-  
|| <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>
+
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
  
<span style="color:#000000;">'''>'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''cargo new </span>arraysdemo'''
+
'''>''' '''cargo new arraysdemo'''
  
<div style="color:#000000;">'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> arraysdemo'''</div>
+
'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> arraysdemo'''
|| <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 arraysdemo '''</span><span style="color:#000000;">and press </span><span style="color:#000000;">'''Enter.'''</span>
+
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
  
<div style="color:#000000;">Open the created project as shown.</div>
+
Type the command '''cargo new arraysdemo '''and press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <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 </span><span style="background-color:#ffffff;">C</span><span style="background-color:#ffffff;color:#000000;">ode file.</span>
+
Open the created project as shown.
|- 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>
+
|| Point to the '''main.rs''' file.
  
<div style="color:#000000;">let arr:[i32;4] = [10,20,30,40];</div>
+
|| In the '''main.rs '''file, copy and paste the code from the '''Code file'''.
 +
|-
 +
|| fn main(){
  
<div style="color:#000000;">println!("First element: {}",arr[0]);</div>
+
let arr:[i32;4] = [10,20,30,40];
  
<div style="color:#000000;">println!("Second element: {}", arr[1]);</div>
+
println!("First element: {}",arr[0]);
  
<div style="color:#000000;">println!("array size is :{}",arr.len());</div>
+
println!("Second element: {}", arr[1]);
  
<div style="color:#000000;">}</div>
+
println!("array size is :{}",arr.len());
  
<div style="color:#000000;">Press Ctrl+s</div>
+
}
|| <div style="color:#000000;">We have declared an array with variable name as '''arr''' and its data type as '''i32''' and size as '''4.'''</div>
+
  
<span style="color:#000000;">The values </span>within<span style="color:#000000;"> the </span><span style="color:#000000;">'''square brackets'''</span><span style="color:#000000;"> are </span><span style="color:#000000;">'''array values'''</span><span style="color:#000000;">.</span>
+
Press Ctrl+s
 +
|| We have declared an array with variable name as '''arr''' and its data type as '''i32''' and size as  4.
 +
 
 +
The values within the  square brackets  are '''array values'''.
  
 
In Rust, an array can store values of any '''datatype'''.
 
In Rust, an array can store values of any '''datatype'''.
  
<span style="background-color:#ffffff;color:#000000;">To display the value of the </span><span style="background-color:#ffffff;color:#000000;">'''nth index'''</span><span style="background-color:#ffffff;color:#000000;"> in an </span><span style="background-color:#ffffff;color:#000000;">'''array, '''</span><span style="background-color:#ffffff;color:#000000;">we need to pass the </span><span style="background-color:#ffffff;color:#000000;">'''index.'''</span><span style="background-color:#ffffff;color:#000000;">Please note that the </span><span style="background-color:#ffffff;color:#000000;">'''array index'''</span><span style="background-color:#ffffff;color:#000000;"> starts from 0.</span>
+
To display the value of the '''nth index''' in an '''array, '''we need to pass the '''index.'''
  
<div style="color:#000000;">Save the program. </div>
+
Please note that the '''array index''' starts from 0.
  
<div style="color:#000000;">Let us execute the code and see the output.</div>
+
Save the program.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <span style="color:#000000;">Click on terminal and select </span><span style="color:#000000;">'''New Terminal.'''</span>
+
  
<span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span><span style="color:#000000;">.</span>
+
Let us execute the code and see the output.
 +
|-
 +
|| Click on terminal and select '''New Terminal.'''
  
<div style="color:#000000;">Point to the output.</div>
+
In the terminal, type '''cargo run'''.
|| <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;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span><span style="color:#000000;">.</span>
+
Point to the output.
 +
|| In the menu bar, click on '''Terminal''' and select '''New Terminal.'''
  
<span style="color:#000000;">It prints the first element as 10, second element as 20</span> and array size as 4.
+
In the terminal, type '''cargo run'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;"></div>
+
  
<div style="color:#000000;"></div>
+
It prints the first element as 10, second element as 20 and array size as 4.
 
+
|-
<div style="color:#000000;"></div>
+
|| println!("array is {:?}"<span style="color:#252525;">, arr[5]);
 
+
<span style="color:#252525;">println!(</span><span style="color:#000000;">"array is {:?}"</span><span style="color:#252525;">, arr[5]);</span>
+
  
 
Press Ctrl+s
 
Press Ctrl+s
|| <div style="color:#000000;">Let us modify the code as shown.</div>
+
|| Let us modify the code as shown.
  
<div style="color:#000000;">We have 4 values in the array.</div>
+
We have 4 values in the array.
  
<div style="color:#000000;">Let us try to access a value greater than 4, say 6th element where there is no value.</div>
+
Let us try to access a value greater than 4, say 6th element where there is no value.
  
<span style="color:#000000;">Add the </span><span style="color:#000000;">'''println'''</span><span style="color:#000000;"> statement as shown.</span>
+
Add the '''println''' statement as shown.  
<div style="color:#000000;">Save the file.</div>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
||
+
  
Point to the error
+
Save the file.
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|-
 +
|| Point to the error
 +
|| In the terminal, type '''cargo run'''.
  
<span style="color:#000000;">We see an error- index out of bounds</span>.
+
We see an error- '''index out of bounds'''.
  
<div style="color:#000000;">So, arrays in Rust are different as compared to other programming languages.</div>
+
So, arrays in Rust are different as compared to other programming languages.
  
<span style="color:#000000;">Th</span>is<span style="color:#000000;"> is because they have a fixed length.</span>
+
This is because they have a fixed length.
  
|- 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 let us see an example of </span><span style="color:#000000;">'''mutable Array'''</span><span style="color:#000000;"> in Rust.</span>
+
|| Next let us see an example of '''mutable Array''' 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;">Delete the existing code.</div>
+
|| Delete the existing code.
  
f<span style="color:#000000;">n main(){&nbsp;&nbsp;</span>
+
fn main(){&nbsp;&nbsp;
  
<div style="color:#000000;">// an array with data type and size</div>
+
// an array with data type and size
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;let mut colors: [&str; 3] = ["black", "blue", "red"];</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;let mut colors: [&str; 3] = ["black", "blue", "red"];
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;println!("Original colors: {:?}", colors);</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;println!("Original colors: {:?}", colors);
  
<div style="color:#000000;">// change the value of the 3rd element in the array</div>
+
// change the value of the 3rd element in the array
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;colors[2] = "green";</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;colors[2] = "green";
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;println!("changed colors = {:?}", colors);</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;println!("changed colors = {:?}", colors);
  
<div style="color:#000000;">&nbsp;&nbsp;}</div>
+
&nbsp;&nbsp;}
|| <div style="color:#000000;">Clear the window and copy and paste the code from the Code file.</div>
+
|| Clear the window and copy and paste the code from the '''Code fil'''e.
  
<span style="color:#000000;">We have declared an array of </span><span style="color:#000000;">'''colors '''</span><span style="color:#000000;">with </span><span style="color:#000000;">'''mut'''</span>.
+
We have declared an array of '''colors '''with '''mut'''.
  
Here the data type and size of the array are specified<span style="color:#000000;">.</span>
+
Here the data type and size of array are specified.
  
<span style="color:#000000;">&nbsp;</span>W<span style="color:#000000;">e have assigned a new value as “</span><span style="color:#000000;">'''green'''</span><span style="color:#000000;">” to the third element in the array.</span>
+
&nbsp;We have assigned a new value as “'''green'''” to the third element in the array.
  
<span style="color:#000000;">As </span>the<span style="color:#000000;"> </span>created array<span style="color:#000000;"> </span>i<span style="color:#000000;">s mutable, it is possible to change the value.</span>
+
As the created array is mutable, it is possible to change the value.
  
<div style="color:#000000;">Save the file.</div>
+
Save the file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Point to the output
 
|| Point to the output
|| <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.
  
 
The output shows the Original colors as black, blue, red.
 
The output shows the Original colors as black, blue, red.
  
And the changed <span style="color:#000000;">color values</span> as black, blue, green.
+
And the changed color values as black, blue, green.
|- 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 let us see some </span>p<span style="color:#000000;">owerful methods used to unlock the full potential of arrays.</span>
+
|| Next let us see some powerful methods used to unlock the full potential of arrays.
  
<div style="color:#000000;">Clear the window and copy and paste the code from the code files.</div>
+
Clear the window and copy and paste the code from the '''code files'''.
|- 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>
+
|| fn main() {
  
<div style="color:#000000;">let arr = [1, 2, 3, 4];</div>
+
let arr = [1, 2, 3, 4];
  
<div style="color:#000000;">// Output: Is empty: false</div>
+
// Output: Is empty: false
  
<div style="color:#000000;">println!("Is empty: {}", arr.is_empty()); </div>
+
println!("Is empty: {}", arr.is_empty());  
  
<div style="color:#000000;">println!("2nd element: {}", arr.get(1).unwrap()); // 2</div>
+
println!("2nd element: {}", arr.get(1).unwrap()); // 2
  
<div style="color:#000000;">println!("First element: {}", arr.first().unwrap()); // 1</div>
+
println!("First element: {}", arr.first().unwrap()); // 1
  
<div style="color:#000000;">println!("Last element: {}", arr.last().unwrap()); // 4</div>
+
println!("Last element: {}", arr.last().unwrap()); // 4
  
<div style="color:#000000;">let (left, right) = arr.split_at(2);</div>
+
let (left, right) = arr.split_at(2);
  
<div style="color:#000000;">println!("Left slice:{:?}, Right slice:{:?}", left, right);</div>
+
println!("Left slice:{:?}, Right slice:{:?}", left, right);
  
<div style="color:#000000;">}</div>
+
}
|| <div style="color:#000000;"></div>
+
|| Let us see the different methods used in arrays.
  
<div style="color:#000000;">Let us see the different methods used in arrays.</div>
+
'''is_empty() ''' method checks if the array has any''' elements'''.
  
<span style="color:#434343;">'''is_empty() '''</span><span style="color:#434343;">method </span>checks if the array has<span style="color:#434343;"> </span><span style="color:#434343;">any</span><span style="color:#434343;">''' elements'''</span><span style="color:#434343;">.</span>
+
Here, '''arr''' has 4 elements, so the result is '''false'''.
  
<span style="color:#000000;">Here, </span><span style="color:#000000;">'''arr'''</span><span style="color:#000000;"> has 4 elements, so the result is </span><span style="color:#000000;">'''false'''</span><span style="color:#000000;">.</span>
+
We can access an element using the '''.get() ''' method.
 
+
We can access an element using the '''.get() '''method.
+
  
 
It takes the index value as the parameter.&nbsp;
 
It takes the index value as the parameter.&nbsp;
Line 216: Line 214:
 
The '''get''' method returns an '''Option enum'''.
 
The '''get''' method returns an '''Option enum'''.
  
<span style="color:#434343;">.</span>'''get(1) '''returns an '''Option<i32> '''with the value at index 1 (which is 2).
+
.'''get(1) '''returns an '''Option<i32> '''with the value at index 1which is 2.
 +
 
 +
.unwrap() extracts the value (2) from '''Some'''.
 +
 
 +
Likewise,'''.first()''' returns '''Some(1)''' that is the first element.
  
<span style="color:#000000;">.unwrap() extracts the value (2) from </span><span style="color:#000000;">'''Some'''</span><span style="color:#000000;">.</span>
+
'''.unwrap()''' extracts 1 from '''Some(1)'''.
  
<span style="color:#188038;">Likewise,.</span><span style="background-color:transparent;color:#188038;">first()</span><span style="background-color:transparent;color:#434343;"> r</span><span style="color:#434343;">eturns </span><span style="color:#188038;">Some(1)</span><span style="color:#434343;"> </span><span style="color:#434343;">that is </span><span style="color:#434343;">the first element.</span>
+
'''.last()''' returns the last element as 4
  
<span style="color:#188038;">.unwrap()</span><span style="color:#434343;"> extracts </span><span style="color:#188038;">1</span><span style="color:#434343;"> from </span><span style="color:#188038;">Some(1)</span><span style="color:#434343;">.</span>
+
.split_at(2) splits the array at index 2, producing two slices:
 +
* left contains the first two elements: that is [1, 2]
 +
* right contains the remaining elements: that is [3, 4]
  
<span style="color:#188038;">.last()</span><span style="color:#434343;"> returns the last element as 4 </span>
+
Save the program.
 +
|-
 +
|| In the terminal, type '''cargo run'''
  
<span style="color:#188038;">.split_at(2)</span><span style="color:#434343;"> splits the array at index </span><span style="color:#188038;">2</span><span style="color:#434343;">, producing two slices:</span>
+
|| In the terminal, type '''cargo run'''
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#188038;">left</span><span style="color:#434343;"> contains the first two elements: </span><span style="color:#434343;">that is</span><span style="color:#188038;">[1, 2]</span></div>
+
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#188038;">right</span><span style="color:#434343;"> contains the remaining elements: </span><span style="color:#434343;">that is</span><span style="color:#434343;"> </span><span style="color:#188038;">[3, 4]</span></div>
+
  
<div style="color:#000000;">Save the program.</div>
+
We can see the left slice and right slice output.
|- 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 terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
|| '''Slide 7'''
  
<div style="color:#000000;"></div>
+
'''Summary'''
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run'''</span>
+
  
<div style="color:#000000;">We can see the left slice and right slice output.</div>
+
In this tutorial, we learn about
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">'''Slide 7'''</div>
+
  
<div style="color:#000000;">'''Summary'''</div>
+
'''Arrays'''
 +
|| This brings us to the end of this tutorial.
  
<div style="color:#000000;">In this tutorial, we learn about</div>
+
Let us summarize.
 +
|-
 +
|| '''Slide 8'''
  
<div style="color:#000000;">'''Arrays'''</div>
+
'''Assignment'''
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
  
<div style="color:#000000;">Let us summarize. </div>
+
[10, 20, 30, 40, 50, 60]
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| As an Assignment, please do the following:
|| <div style="color:#000000;">'''Slide 8'''</div>
+
  
<div style="color:#000000;">'''Assignment'''</div>
+
* Declare an array of 6 integers
|| <div style="color:#000000;">As an Assignment, please do the following:</div>
+
* Use a loop to print all elements in the array
 +
* Create a slice of the array from index 2 to 4
 +
* Print the slice using {:?}
  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Declare an array of 6 integers: [10, 20, 30, 40, 50, 60]</div>
+
|-  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Use a loop to print all elements in the array</div>
+
|| '''Slide 9
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Create a slice of the array from index 2 to 4 </div>
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Print the slice using {:?}</div>
+
  
|- 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'''
+
|| Thank you for joining.
| style="color:#000000;" | Thank you for joining.
+
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Latest revision as of 11:11, 12 September 2025

Visual Cue Narration
Slide 1

Title Slide

Welcome to the Spoken Tutorial on Arrays in Rust.
Slide 2

Learning Objectives

In this tutorial, we will learn about:
  • Arrays
Slide 3

System Requirements

To record this tutorial I am using:
  • Ubuntu Linux OS version 22.04
  • Rust version 1.80.0
  • Visual Studio Code version 1.19.0
Slide 4

Prerequisites

To follow this tutorial, you should be familiar with compiling and running Rust programs.
Slide 5

Code Files

  • arraysdemo.rs
  • 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

Arrays

  • Arrays in Rust are collections of values of the same data type and have a fixed size
  • Arrays once initialized cannot be resized
  • An array declaration allocates sequential memory blocks
Let us understand how arrays work in a Rust program.
Open Visual Studio 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 arraysdemo

In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> arraysdemo

Let us go to our working directory MyRustProject as explained earlier.

Type the command cargo new arraysdemo 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 arr:[i32;4] = [10,20,30,40];

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

println!("Second element: {}", arr[1]);

println!("array size is :{}",arr.len());

}

Press Ctrl+s

We have declared an array with variable name as arr and its data type as i32 and size as 4.

The values within the square brackets are array values.

In Rust, an array can store values of any datatype.

To display the value of the nth index in an array, we need to pass the index.

Please note that the array index starts from 0.

Save the program.

Let us execute the code and see the output.

Click on terminal and select New Terminal.

In the terminal, type cargo run.

Point to the output.

In the menu bar, click on Terminal and select New Terminal.

In the terminal, type cargo run.

It prints the first element as 10, second element as 20 and array size as 4.

println!("array is {:?}", arr[5]);

Press Ctrl+s

Let us modify the code as shown.

We have 4 values in the array.

Let us try to access a value greater than 4, say 6th element where there is no value.

Add the println statement as shown.

Save the file.

Point to the error In the terminal, type cargo run.

We see an error- index out of bounds.

So, arrays in Rust are different as compared to other programming languages.

This is because they have a fixed length.

Next let us see an example of mutable Array in Rust.
Delete the existing code.

fn main(){  

// an array with data type and size

    let mut colors: [&str; 3] = ["black", "blue", "red"];

    println!("Original colors: {:?}", colors);

// change the value of the 3rd element in the array

    colors[2] = "green";

    println!("changed colors = {:?}", colors);

  }

Clear the window and copy and paste the code from the Code file.

We have declared an array of colors with mut.

Here the data type and size of array are specified.

 We have assigned a new value as “green” to the third element in the array.

As the created array is mutable, it is possible to change the value.

Save the file.

Point to the output In the terminal, type cargo run and see the output.

The output shows the Original colors as black, blue, red.

And the changed color values as black, blue, green.

Next let us see some powerful methods used to unlock the full potential of arrays.

Clear the window and copy and paste the code from the code files.

fn main() {

let arr = [1, 2, 3, 4];

// Output: Is empty: false

println!("Is empty: {}", arr.is_empty());

println!("2nd element: {}", arr.get(1).unwrap()); // 2

println!("First element: {}", arr.first().unwrap()); // 1

println!("Last element: {}", arr.last().unwrap()); // 4

let (left, right) = arr.split_at(2);

println!("Left slice:{:?}, Right slice:{:?}", left, right);

}

Let us see the different methods used in arrays.

is_empty() method checks if the array has any elements.

Here, arr has 4 elements, so the result is false.

We can access an element using the .get() method.

It takes the index value as the parameter. 

The get method returns an Option enum.

.get(1) returns an Option<i32> with the value at index 1, which is 2.

.unwrap() extracts the value (2) from Some.

Likewise,.first() returns Some(1) that is the first element.

.unwrap() extracts 1 from Some(1).

.last() returns the last element as 4

.split_at(2) splits the array at index 2, producing two slices:

  • left contains the first two elements: that is [1, 2]
  • right contains the remaining elements: that is [3, 4]

Save the program.

In the terminal, type cargo run In the terminal, type cargo run

We can see the left slice and right slice output.

Slide 7

Summary

In this tutorial, we learn about

Arrays

This brings us to the end of this tutorial.

Let us summarize.

Slide 8

Assignment

[10, 20, 30, 40, 50, 60]

As an Assignment, please do the following:
  • Declare an array of 6 integers
  • Use a loop to print all elements in the array
  • Create a slice of the array from index 2 to 4
  • Print the slice using {:?}
Slide 9

Thank You

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat