Rust/C2/Data-Types-in-Rust/English
Title of the script: Data Types
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: Rust, variables, data types, scalar, integers, float, boolean, character
|
|
Slide: Title | Welcome to the Spoken Tutorial on “Data Types in Rust”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other editor of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Data Types in Rust |
We’ll see them now. |
Slide: Data Types in Rust | There are 2 major data types namely scalar and compound. |
[Only narration] | Do remember that Rust is a statically typed language.
|
Slide: Scalar Types |
Now let us take an example of a scalar data type and see how it works in Rust. |
Press Ctrl+Alt+T keys | Open the terminal by pressing Ctrl,Alt and T keys simultaneously on the keyboard.
|
Only Narration | Here onwards, please remember to press the Enter key after typing each command. |
[Terminal] Type:
[Enter] |
Using cd command go to the Rust practice folder which we created earlier. |
[Terminal] Type:
|
Let us create a new project named scalar_types.
|
Open Visual Studio Code editor. | You may use any editor of your choice.
|
[Editor]
Open Folder -> scalar_types |
Open the created project by clicking on the Open folder link in the Welcome page.
|
[Editor]
Click on scalar_types |
Under the EXPLORER section, expand the project folder “scalar_types” by clicking on it. |
[Editor] Expand src and click on main.rs | Then expand src and open then main.rs file. |
[Editor] Type:
let mut a = 2147483647; a = a + 1; print!(“The value of a is {}”,a); } |
In the editor, replace the code as shown. |
[Editor] Highlight:
let mut a = 2147483647; |
Here the variable a is declared with value 2147483647, which is an integer variable.
|
[Editor] Highlight:
|
Next we are experimenting by trying to add 1 to the maximum value. |
[Editor] Highlight:
|
Using the print method we are printing the variable’s value. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Go to the project folder scalar_types using the cd command. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
Here, we see an error - attempt to add with overflow. |
[Terminal] Type:
|
If we try to compile using cargo build, it compiles successfully. |
[Terminal] Highlight:
|
But when we run the project we get an error as the addition takes place in runtime. |
[Only narration] | This reminds us that the integer variable is 32-bit.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
|
Update the variable declaration as shown. |
[Editor] Highlight:
|
Here we are telling the compiler that the initialized variable is of 64-Bit. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
We can see the output.
|
Slide: Float |
|
Slide: Float [show]
let a = 1.0; |
|
Slide: Float [show]
let a:f32 = 1.0; |
|
Slide: Boolean |
|
Slide: Boolean
[show] let a:bool = true; |
|
Slide: Character
|
|
Next, let’s learn about Compound Types. | |
Slide: Compound Data Types |
|
Side: Tuples |
|
Side: Tuples |
Now let’s work on an example and understand this better. |
[Terminal] Type:
|
Using cd command go to the Rust practice folder. |
[Terminal] Type:
|
Let us create a new project named tuples.
|
Open Visual Studio Code editor. | Switch to the editor. |
Help -> Welcome | Go to the Help menu and select Welcome.
|
[Editor]
Open Folder -> tuples |
Open the created project by clicking on the Open folder link in the Welcome page.
|
[Editor] Click on tuples | Under the EXPLORER section, expand the project folder “tuples” by clicking on it. |
[Editor] Expand src and click on main.rs | Then expand src and open the main.rs file. |
[Editor] Type:
let tup:(i32,i32,i32) = (1,2,3); let (x,y,z) = tup; println!(‘x - {} , Y - {} , Z - {}‘, x,y,z); } |
In the editor, replace the code as shown. |
[Editor] Highlight:
|
Here, we first declare a tuple with three 32-bit integer values 1,2,3 respectively. |
[Editor] Highlight:
|
Then we declare 3 meaningful variables - x, y and z and then assign the variables to the already created tuple. |
[Editor] Highlight:
|
Using the print method we will print the respective values. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
cd tuples [Enter] |
Go to the project folder tuples using the cd command. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
We can see the output as x=1, y=2 and z=3.
|
Switch to Editor | Switch back to the editor. |
[Editor] Highlight:
|
tup is a variable which has three values 1, 2 and 3. |
[Editor] Highlight:
|
We need to give these 3 values meaningful variable names.
|
[Editor] Type:
println!(“A = {}”,a); |
Type this code inside the main function below the last line as shown. |
[Editor] Highlight:
|
tup.0 - this is how we directly access a tuple using period.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
We see the output of A, x, y and z. |
Slide: Arrays
|
Another way of storing multiple values is using an array.
|
Slide: Arrays |
Let us take an example and understand this better. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
println!(“First Value = {}”, numbers[0]); |
In the editor, type this code inside the main function below the last line as shown.
|
[Editor] Highlight:
|
Here, we have declared a variable with the name numbers.
|
[Editor] Highlight:
|
Here, we are printing the first value by accessing the array starting with index 0. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
We see the output as the first value in the array is displayed. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
|
Now type this code, inside the main function below the last line
as shown. |
[Editor] Highlight:
|
We now have 5 values in the array.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run |
[Terminal] Highlight:
|
We see an error- index out of bounds, the length is 5 but the index is 11.
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education, Government of India |
Slide: Thanks | This is Jayesh signing off. Thank you for joining |