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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 34: Line 34:
  
 
'''Code Files'''
 
'''Code Files'''
 +
 +
*'''arraysdemo.rs'''
 
||
 
||
 
* The following code file is required to practise this tutorial.
 
* The following code file is required to practise this tutorial.
*'''arraysdemo.rs'''
+
 
 
* This file is provided in the '''Code Files''' link of this tutorial page.
 
* This file is provided in the '''Code Files''' link of this tutorial page.
  
Line 65: Line 67:
  
 
'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> arraysdemo'''
 
'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> arraysdemo'''
 +
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
 
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
  
Line 73: Line 76:
 
|| Point to the '''main.rs''' file.
 
|| Point to the '''main.rs''' file.
  
|| In the '''main.rs '''file, copy and paste the code from the Code file.
+
|| In the '''main.rs '''file, copy and paste the code from the '''Code file'''.
 
|-  
 
|-  
 
|| fn main(){
 
|| fn main(){
Line 88: Line 91:
  
 
Press Ctrl+s
 
Press Ctrl+s
|| We have declared an array with variable name as '''arr''' and its data type as '''i32''' and size as '''4.'''
+
|| 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'''.
+
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'''.
  
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.
+
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.  
 
Save the program.  
Line 120: Line 125:
 
Let us try to access a value greater than 4, say 6th element where there is no value.
 
Let us try to access a value greater than 4, say 6th element where there is no value.
  
Add the '''println''' statement as shown.
+
Add the '''println''' statement as shown.  
 +
 
 
Save the file.
 
Save the file.
 
|-  
 
|-  
 
|| Point to the error
 
|| Point to the error
|| In the terminal, type '''cargo run'''
+
|| In the terminal, type '''cargo run'''.
  
We see an error- index out of bounds.
+
We see an error- '''index out of bounds'''.
  
 
So, arrays in Rust are different as compared to other programming languages.
 
So, arrays in Rust are different as compared to other programming languages.
Line 153: Line 159:
  
 
  }
 
  }
|| Clear the window and copy and paste the code from the Code file.
+
|| Clear the window and copy and paste the code from the '''Code fil'''e.
  
 
We have declared an array of '''colors '''with '''mut'''.
 
We have declared an array of '''colors '''with '''mut'''.
  
Here the data type and size of the array are specified.
+
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.
 
 We have assigned a new value as “'''green'''” to the third element in the array.
Line 175: Line 181:
 
|| Next let us see some powerful methods used to unlock the full potential of arrays.
 
|| 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.
+
Clear the window and copy and paste the code from the '''code files'''.
 
|-  
 
|-  
 
|| fn main() {
 
|| fn main() {
Line 198: Line 204:
 
||  Let us see the different methods used in arrays.
 
||  Let us see the different methods used in arrays.
  
'''is_empty() '''method checks if the array has any''' elements'''.
+
'''is_empty() ''' method checks if the array has any''' elements'''.
  
 
Here, '''arr''' has 4 elements, so the result is '''false'''.
 
Here, '''arr''' has 4 elements, so the result is '''false'''.
  
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. 
 
It takes the index value as the parameter. 
Line 208: Line 214:
 
The '''get''' method returns an '''Option enum'''.
 
The '''get''' method returns an '''Option enum'''.
  
.'''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'''.
 
.unwrap() extracts the value (2) from '''Some'''.
  
Likewise,.first() returns Some(1) that is the first element.
+
Likewise,'''.first()''' returns '''Some(1)''' that is the first element.
  
.unwrap() extracts 1 from Some(1).
+
'''.unwrap()''' extracts 1 from '''Some(1)'''.
  
.last() returns the last element as 4  
+
'''.last()''' returns the last element as 4  
  
 
.split_at(2) splits the array at index 2, producing two slices:
 
.split_at(2) splits the array at index 2, producing two slices:
Line 225: Line 231:
 
|-  
 
|-  
 
|| In the terminal, type '''cargo run'''
 
|| In the terminal, type '''cargo run'''
 
  
 
|| In the terminal, type '''cargo run'''
 
|| In the terminal, type '''cargo run'''
Line 245: Line 250:
  
 
'''Assignment'''
 
'''Assignment'''
 +
 +
[10, 20, 30, 40, 50, 60]
 
|| As an Assignment, please do the following:
 
|| As an Assignment, please do the following:
  
* Declare an array of 6 integers: [10, 20, 30, 40, 50, 60]
+
* Declare an array of 6 integers
 
* Use a loop to print all elements in the array
 
* Use a loop to print all elements in the array
 
* Create a slice of the array from index 2 to 4  
 
* Create a slice of the array from index 2 to 4  

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