Difference between revisions of "Rust/C2/Data-Types-in-Rust/English"
(Created page with "Title of the script: Data Types Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: Rust, variables, data types, scalar, integers, float, boole...") |
Nancyvarkey (Talk | contribs) |
||
Line 17: | Line 17: | ||
|- | |- | ||
|| Slide: Title | || Slide: Title | ||
− | || Welcome to the | + | || Welcome to the spoken tutorial on “'''Data Types '''in''' Rust'''”. |
|- | |- | ||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* '''Supported Data Types''' | * '''Supported Data Types''' | ||
* '''Scalar Data Types ''' | * '''Scalar Data Types ''' | ||
* '''Compound Data Types '''and | * '''Compound Data Types '''and | ||
− | * | + | * Its '''types''' in '''Rust'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 49: | Line 45: | ||
* You should be familiar with '''compiling''' and '''running Rust''' files'''.''' | * You should be familiar with '''compiling''' and '''running Rust''' files'''.''' | ||
* If not, please go through the prerequisite '''Rust''' tutorials on this website. | * If not, please go through the prerequisite '''Rust''' tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
Line 58: | Line 52: | ||
* Pls download and extract the file. | * Pls download and extract the file. | ||
* Make a copy and then use it for practising. | * Make a copy and then use it for practising. | ||
− | |||
− | |||
|- | |- | ||
Line 67: | Line 59: | ||
* It tells the '''compiler '''what kind of '''data '''is being specified. | * It tells the '''compiler '''what kind of '''data '''is being specified. | ||
* This helps the '''compiler '''know how to work with that '''data'''. | * This helps the '''compiler '''know how to work with that '''data'''. | ||
− | * And, helps optimize for efficient & faster runtime execution. | + | * And, helps optimize for efficient & faster '''runtime execution.''' |
− | * There are a number of | + | * There are a number of '''in-built Data Types''' in '''Rust'''. |
We’ll see them now. | We’ll see them now. | ||
Line 84: | Line 76: | ||
− | The '''compiler '''can usually infer what '''data type''' is used based on the value and improve runtime efficiency | + | The '''compiler '''can usually infer what '''data type''' is used based on the value and improve '''runtime''' efficiency. |
|- | |- | ||
Line 127: | Line 119: | ||
Type the '''command''' as shown. | Type the '''command''' as shown. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 136: | Line 125: | ||
− | I will use '''Visual Studio Code editor '''for this demonstration | + | I will use '''Visual Studio Code editor '''for this demonstration. |
|- | |- | ||
Line 163: | Line 152: | ||
|- | |- | ||
|| <nowiki>[Editor] </nowiki>Expand '''src '''and click on '''main.rs''' | || <nowiki>[Editor] </nowiki>Expand '''src '''and click on '''main.rs''' | ||
− | || Then expand '''src''' and open then main.rs file. | + | || Then expand '''src''' and open then '''main.rs''' file. |
|- | |- | ||
Line 187: | Line 176: | ||
− | By default, the '''integer variable''' is '''32-bit (i32)''', which is a signed integer type and can hold both '''+ve''' and '''-ve''' values. | + | By default, the '''integer variable''' is '''32-bit (i32)''', which is a '''signed integer type''' and can hold both '''+ve''' and '''-ve''' values. |
− | '''Rust '''even offers '''unsigned integers ''' | + | '''Rust '''even offers '''unsigned integers type'''. |
− | You can explicitly typecast to '''u32''' if you don’t want the value to go below '''zero'''. | + | You can explicitly '''typecast''' to '''u32''' if you don’t want the value to go below '''zero'''. |
Line 210: | Line 199: | ||
'''print!("The value of a is {}",a);''' | '''print!("The value of a is {}",a);''' | ||
− | || Using the '''print method''' we are printing the variable’s value. | + | || Using the '''print method''' we are printing the '''variable’s''' value. |
|- | |- | ||
Line 314: | Line 303: | ||
|| Slide: Float | || Slide: Float | ||
|| | || | ||
− | * '''Rust''' has 2 primitive '''types''' for '''Float '''similar to '''integers''' | + | * '''Rust''' has 2 primitive '''types''' for '''Float '''similar to '''integers''' |
− | + | *'''f32 and f64''' | |
− | + | ||
|- | |- | ||
Line 324: | Line 312: | ||
|| | || | ||
* Here the numbers are with decimal points.For '''float''', by default '''Rust''' allocates '''64-bits''' | * Here the numbers are with decimal points.For '''float''', by default '''Rust''' allocates '''64-bits''' | ||
− | * Here for '''let a = 1.0''' ''' | + | * Here for '''let a = 1.0''' '''Rust''' allocates '''64-bits''' of memory. |
− | + | ||
− | + | ||
|- | |- | ||
Line 334: | Line 320: | ||
|| | || | ||
* If you want '''Rust''' to allocate '''32-bits '''of''' memory''', we need to explicitly '''typecast '''to '''32-bits '''using''' f32'''. | * If you want '''Rust''' to allocate '''32-bits '''of''' memory''', we need to explicitly '''typecast '''to '''32-bits '''using''' f32'''. | ||
− | |||
− | |||
|- | |- | ||
Line 342: | Line 326: | ||
* As in most '''programming languages''', in '''Rust''' also we have two possible values for '''boolean''' | * As in most '''programming languages''', in '''Rust''' also we have two possible values for '''boolean''' | ||
* '''True '''and''' False''' | * '''True '''and''' False''' | ||
− | |||
− | |||
|- | |- | ||
Line 355: | Line 337: | ||
* Typically these '''variables''' would be used whenever we set '''flags'''. | * Typically these '''variables''' would be used whenever we set '''flags'''. | ||
* Depending upon the state of '''flag''' we allow further '''execution''' of the '''program'''. | * Depending upon the state of '''flag''' we allow further '''execution''' of the '''program'''. | ||
− | |||
− | |||
|- | |- | ||
Line 366: | Line 346: | ||
* So far we have worked only with numbers. | * So far we have worked only with numbers. | ||
* '''Rust '''also supports letters. | * '''Rust '''also supports letters. | ||
− | * The '''Character ''' | + | * The '''Character type''' is the most primitive alphabetic '''type'''. |
− | * The value should be declared between single quotes | + | * The value should be '''declared''' between single quotes |
− | * Here’s an example of how a character can be declared. | + | * Here’s an example of how a '''character''' can be '''declared'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 380: | Line 358: | ||
|| | || | ||
* '''Compound Types '''group multiple values of other '''types''' into one '''type'''. | * '''Compound Types '''group multiple values of other '''types''' into one '''type'''. | ||
− | * '''Rust''' has two primitive '''Compound types '''namely '''Tuples''' and '''Arrays.''' | + | * '''Rust''' has two primitive '''Compound types '''namely, '''Tuples''' and '''Arrays.''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 390: | Line 366: | ||
* A '''Tuple''' can contain a variety of '''types'''. | * A '''Tuple''' can contain a variety of '''types'''. | ||
* We create '''Tuples''' by writing a comma separated list of values within parentheses. | * We create '''Tuples''' by writing a comma separated list of values within parentheses. | ||
− | |||
− | |||
|- | |- | ||
Line 427: | Line 401: | ||
− | We are back to the '''Welcome | + | We are back to the '''Welcome page ''' |
|- | |- | ||
Line 472: | Line 446: | ||
let tup:(i32,i32,i32) = (1,2,3); | let tup:(i32,i32,i32) = (1,2,3); | ||
− | || Here, we first declare a '''tuple''' with three '''32-bit integer '''values 1,2,3 respectively. | + | || Here, we first '''declare''' a '''tuple''' with three '''32-bit integer '''values 1,2,3 respectively. |
|- | |- | ||
Line 514: | Line 488: | ||
'''x - 1 , Y - 2 , Z - 3''' | '''x - 1 , Y - 2 , Z - 3''' | ||
− | || We can see the output as '''x=1, y=2 and z=3'''. | + | || We can see the output as '''x=1, y=2''' and '''z=3'''. |
Line 590: | Line 564: | ||
|| Another way of storing multiple values is using an '''array'''. | || Another way of storing multiple values is using an '''array'''. | ||
− | |||
* Unlike a '''tuple''', every '''element''' in an '''array''' should have the same '''data type'''. | * Unlike a '''tuple''', every '''element''' in an '''array''' should have the same '''data type'''. | ||
* '''Arrays''' in '''Rust''' are different as compared to other '''programming languages''' | * '''Arrays''' in '''Rust''' are different as compared to other '''programming languages''' | ||
* As they have a fixed length. | * As they have a fixed length. | ||
− | |||
− | |||
|- | |- | ||
Line 618: | Line 589: | ||
<nowiki>println!(“First Value = {}”, numbers[0]);</nowiki> | <nowiki>println!(“First Value = {}”, numbers[0]);</nowiki> | ||
|| In the '''editor''', type this code inside the '''main function''' below the last line as shown. | || In the '''editor''', type this code inside the '''main function''' below the last line as shown. | ||
− | |||
− | |||
Line 627: | Line 596: | ||
<nowiki>let numbers = [1,2,3,4,5];</nowiki> | <nowiki>let numbers = [1,2,3,4,5];</nowiki> | ||
− | || Here, we have declared a '''variable''' with the name '''numbers. ''' | + | || Here, we have '''declared''' a '''variable''' with the name '''numbers. ''' |
Line 700: | Line 669: | ||
'''cargo run '''<nowiki>[Enter]</nowiki> | '''cargo run '''<nowiki>[Enter]</nowiki> | ||
− | || Now type '''cargo run''' | + | || Now type '''cargo run'''. |
|- | |- | ||
Line 725: | Line 694: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* '''Supported Data Types '''and | * '''Supported Data Types '''and | ||
* '''Scalar Data Types ''' | * '''Scalar Data Types ''' | ||
* '''Compound Data Types''' and | * '''Compound Data Types''' and | ||
* Its '''types '''in '''Rust'''. | * Its '''types '''in '''Rust'''. | ||
− | |||
− | |||
|- | |- | ||
Line 740: | Line 705: | ||
* Go to the project folder '''rust-assignment''' | * Go to the project folder '''rust-assignment''' | ||
* In the '''main.rs''' file | * In the '''main.rs''' file | ||
− | ** Initialize a '''variable '''named '''m '''and assign an '''array '''to it | + | ** '''Initialize''' a '''variable '''named '''m '''and assign an '''array '''to it |
** The '''array '''should contain '''5 numbers '''as''' elements''' | ** The '''array '''should contain '''5 numbers '''as''' elements''' | ||
** Print the '''elements '''of the '''array''' | ** Print the '''elements '''of the '''array''' | ||
* '''Compile '''and '''execute '''the project | * '''Compile '''and '''execute '''the project | ||
* Observe the output in the '''Terminal''' | * Observe the output in the '''Terminal''' | ||
− | |||
− | |||
|- | |- | ||
Line 753: | Line 716: | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
* Please download and watch it | * Please download and watch it | ||
− | |||
− | |||
|- | |- | ||
Line 761: | Line 722: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- |
Revision as of 20:53, 24 January 2021
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 |