Rust-Programming-Language/C2/Arrays/English
| Visual Cue | Narration |
| Slide 1 | Welcome to the Spoken Tutorial on Arrays in Rust. |
| Slide 2
Learning Objectives
|
In this tutorial, we will learn about:
|
| Slide 3
System Requirements
|
To record this tutorial I am using:
|
| Slide 4
Prerequisites
|
To follow this tutorial, you should be familiar with compiling and running Rust programs. |
| Slide 5
Code Files
|
|
| Slide 6
Arrays
|
|
| 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 the 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:
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
|
As an Assignment, please do the following:
|
| Slide 9 Thank You | Thank you for joining. |