Difference between revisions of "Rust-Programming-Language/C2/Tuples-and-Struct/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' | style="color:#000000;" | Welcome to the Spoken Tutorial on '''Tuples and Struct in Rust.''' |- st...")
 
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
| style="color:#000000;" | Welcome to the Spoken Tutorial on '''Tuples and Struct 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;">'''Slide 2'''</div>
 
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
'''Title Slide'''
 +
|| Welcome to the Spoken Tutorial on '''Tuples and Struct 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;">Tuples and</div>
+
* '''Tuples''' and
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Struct</div>
+
* '''Struct'''
  
|- 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’m 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 a '''Rust''' program.
+
|| To follow this tutorial, you should be familiar with compiling and running a '''Rust''' program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide''' '''5'''</div>
+
|| '''Slide 5'''
  
<div style="color:#000000;">'''Code Files'''</div>
+
'''Code Files'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">The following code file is required to practise this tutorial.</div>
+
* The following code file is required to practise this tutorial.
* <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>
+
* This file is provided in the '''Code Files''' link of this tutorial page.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 6'''</div>
+
|| '''Slide 6'''
  
<div style="color:#000000;">'''Tuples'''</div>
+
'''Tuples'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Tuple is a compound data type</div>
+
* '''Tuple''' is a compound data type
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">It is a group of values of different types, stored in a single variable.</div>
+
* It is a group of values of different types, stored in a single variable.
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Tuples have a fixed length - once declared they cannot grow or shrink in size.</div>
+
* '''Tuple'''s have a fixed length - once declared they cannot grow or shrink in size.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.118cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
| style="color:#000000;" | Let us understand how tuples work in a '''Rust''' program.
+
|| Let us understand how tuples work in a '''Rust''' program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" | Open '''Visual''' '''code editor'''
+
|| Open '''Visual Studio code editor.'''
| style="color:#000000;" | Open the '''Visual Studio code editor.'''
+
|| 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;"
+
|-
|| <div style="color:#000000;">In the menu bar, click on '''Terminal''' and select''' New Terminal'''.</div>
+
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
  
|| <div style="color:#000000;">In the menu bar, click on '''Terminal''' and select''' New Terminal'''.</div>
+
|| 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;"
+
|-
|| <div style="color:#000000;">'''> cd''' '''Desktop'''/'''MyRustProject'''</div>
+
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
  
<div style="color:#000000;">'''>''' '''cargo new tuplesdemo'''</div>
+
>''' cargo new tuplesdemo'''
  
<div style="color:#000000;">'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> tuplesdemo'''</div>
+
'''In the menu bar, File >> Open folder
|| <div style="color:#000000;">Let us go to our working directory '''MyRustProject''' as explained earlier.</div>
+
  
<div style="color:#000000;">Type the command '''cargo new tuplesdemo '''and press '''Enter.'''</div>
+
>> Desktop >> MyRustProject >> tuplesdemo'''
 +
|| 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 tuplesdemo ''' and press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">Point to the '''main.rs''' file.</div>
+
  
| style="color:#000000;" | <span style="background-color:#ffffff;">In the </span><span style="background-color:#ffffff;">'''main.rs '''</span><span style="background-color:#ffffff;">file, copy and paste the code from the Code 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 person: (f64, u8, bool) = (18000.00, 30, true);</div>
+
|| In the '''main.rs ''' file, copy and paste the code from the '''Code file'''.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;">println!("salary: {}", person.0);</div>
+
let person: (f64, u8, bool) = (18000.00, 30, true);
  
<div style="color:#000000;">println!("Age: {}", person.1);</div>
+
println!("salary: {}", person.0);
  
<div style="color:#000000;">println!("Is Active: {}", person.2);</div>
+
println!("Age: {}", person.1);
  
<div style="color:#000000;">// Destructuring a tuple</div>
+
println!("Is Active: {}", person.2);
  
<div style="color:#000000;">let(salary,age,status) =person;</div>
+
// Destructuring a tuple
  
<div style="color:#000000;">println!("Destructured - Salary: {}, Age: {}, Status: {}", salary, age, status);</div>
+
let(salary,age,status) =person;
  
<div style="color:#000000;">}</div>
+
println!("Destructured - Salary: {}, Age: {}, Status: {}", salary, age, status);
  
<div style="color:#000000;">Press Ctrl+s</div>
+
}
|| <div style="color:#000000;">We have declared a variable called '''person''' with three different data types.</div>
+
  
<div style="color:#000000;">Here the tuple is declared with floating-point, integer and boolean data type.</div>
+
Press Ctrl+S
 +
|| We have declared a variable called '''person''' with three different data types.
  
<div style="color:#000000;">We can access tuple elements using dot notation followed by the index of the element.</div>
+
Here the '''tuple'''  is declared with '''floating-point''', '''integer''' and '''boolean data type'''.
  
<div style="color:#000000;">0 for the first element, 1 for the second, and so on.</div>
+
We can access '''tuple''' elements using dot notation followed by the index of the element.
  
<div style="color:#000000;">'''Destructuring''' means it <span style="background-color:#ffffff;">breaks the tuple into individual variables.</span></div>
+
0 for the first element, 1 for the second, and so on.
  
<div style="color:#000000;">Here, salary<span style="background-color:#ffffff;"> becomes "</span>18000<span style="background-color:#ffffff;">", age becomes 30, and status becomes “true”</span></div>
+
'''Destructuring''' means it breaks the '''tuple''' into individual variables.
  
<div style="color:#000000;">It prints values stored in the new variables.</div>
+
Here, salary becomes "18000", age becomes 30, and status becomes “true”.
  
<div style="color:#000000;">Save the program.</div>
+
It prints values stored in the new variables.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">Click on terminal and select '''New Terminal.'''</div>
+
  
<div style="color:#000000;">In the terminal, type '''cargo run'''.</div>
+
Save the program.
 +
|-
 +
|| Click on terminal and select '''New Terminal.'''
  
<div style="color:#000000;">Point to the output.</div>
+
In the terminal, type '''cargo run'''.
| style="color:#000000;" | Let us execute the code and see the output as shown,
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
| style="color:#000000;" | Next let us see how to use '''Tuple''' as a return value.
+
|- 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>
+
  
<div style="color:#000000;">fn get_coordinates() -> (i32, i32) {</div>
+
Point to the output.
 +
|| Let us execute the code and see the output as shown,
 +
|-
 +
||
 +
|| Next let us see how to use '''Tuple''' as a return value.
 +
|-
 +
|| Delete the existing code.
  
<div style="color:#000000;">(10, 20)</div>
+
fn get_coordinates() -> (i32, i32) {
  
<div style="color:#000000;">}</div>
+
(10, 20)
  
<div style="color:#000000;">fn main() {</div>
+
}
  
<div style="color:#000000;">let (x, y) = get_coordinates();</div>
+
fn main() {
  
<div style="color:#000000;">println!("X: {}, Y: {}", x, y);</div>
+
let (x, y) = get_coordinates();
  
<div style="color:#000000;">}</div>
+
println!("X: {}, Y: {}", x, y);
|| <div style="color:#000000;">Clear the window and copy and paste the code from the Code file.</div>
+
  
<div style="color:#000000;">This code shows that '''Tuples''' are commonly used to return multiple values from a function.</div>
+
}
 +
|| Clear the window and copy and paste the code from the '''Code file'''.
  
<div style="color:#000000;"></div>
+
This code shows that '''Tuples''' are commonly used to return multiple values from a function.
  
<div style="color:#000000;"></div>
+
Save the file.
  
<div style="color:#000000;">Save the file.</div>
+
|-
 +
|| Point to the output.
 +
|| Check the output as shown.
 +
|-
 +
||
 +
|| Next we will see about '''Rust struct''' or '''structures'''.
 +
|-
 +
||  '''Slide 7'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
'''Struct'''
| style="color:#000000;" |
+
| style="color:#000000;" | Check the output as shown.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
| style="color:#000000;" | <span style="background-color:#ffffff;">Next we will see about </span><span style="background-color:#ffffff;">'''Rust'''</span><span style="background-color:#ffffff;"> </span><span style="background-color:#ffffff;">'''struct'''</span><span style="background-color:#ffffff;"> or </span><span style="background-color:#ffffff;">'''structures'''</span><span style="background-color:#ffffff;">.</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:</div>
+
 
+
<div style="color:#000000;">Struct</div>
+
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Structs are similar to tuples</div>
+
* '''Struct'''s are similar to '''tuple'''s
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Structs can also hold multiple related values, of different data types.</div>
+
* '''Struct'''s can also hold multiple related values, of different data types.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
||  
|| <div style="color:#000000;">Switch back to the '''Visual studio code editor.'''</div>
+
|| Switch back to the '''Visual studio code editor.'''
  
<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 file'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| <div style="color:#000000;">struct User {</div>
+
|| struct User {
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;active: bool,</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;active: bool,
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;username: String,</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;username: String,
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;email: String,</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;email: String,
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">fn main() {</div>
+
fn main() {
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;let user1 = User {</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;let user1 = User {
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;active: true,</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;active: true,
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: String::from("someusername123"),</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: String::from("someusername123"),
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email: String::from("someone@example.com"),</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email: String::from("someone@example.com"),
  
<div style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;};</div>
+
&nbsp;&nbsp;&nbsp;&nbsp;};
  
<div style="color:#000000;">println!(“{:?}”,user1.email);</div>
+
println!(“{:?}”,user1.email);
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">Press Ctrl+s</div>
+
Press Ctrl+S
|| <div style="color:#000000;">Here’s an example of a struct.</div>
+
|| Here’s an example of a '''struct'''.
  
<div style="color:#000000;">We define a struct named '''User.'''</div>
+
We define a struct named '''User.'''
  
<div style="color:#000000;">The fields with their names and data types are specified within the curly braces.</div>
+
The fields with their names and data types are specified within the curly braces.
  
<div style="color:#000000;">It groups together multiple pieces of data related to a user account.</div>
+
It groups together multiple pieces of data related to a user account.
  
<div style="color:#000000;"></div>
+
'''user1''' is a variable of type '''User'''.
  
<div style="color:#000000;"></div>
+
'''User ''' creates an instance of the '''User struct '''and assigns it to '''user1''' variable.
  
<div style="color:#000000;"></div>
+
We have the flexibility to assign key value pairs in any order, as '''struct''' is just a template.
  
<div style="color:#000000;"></div>
+
To get a specific value from a '''struct''', we use dot notation.
  
<div style="color:#000000;">'''user1''' is a variable of type '''User'''.</div>
+
For example, to access this user’s email address, we use '''user1.email'''
  
<div style="color:#000000;">'''User '''creates an instance of the '''User struct '''and assigns it to '''user1''' variable.</div>
+
Save the program.
 +
|-
 +
|| In the terminal, type '''cargo run'''
  
<div style="color:#000000;">We have the flexibility to assign key value pairs in any order, as struct is just a template.</div>
+
|| In the terminal, type '''cargo run '''to see the output.
  
<div style="color:#000000;">To get a specific value from a struct, we use dot notation.</div>
+
|-
 +
||
 +
|| Next we will see about the declaring methods for '''struct'''.
  
<div style="color:#000000;">For example, to access this user’s email address, we use '''user1.email'''</div>
+
Copy and paste the code from the code file.
 +
|-
 +
|| struct Rectangle {
  
<div style="color:#000000;">Save the program.</div>
+
width: u32,
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">In the terminal, type '''cargo run'''</div>
+
  
|| <div style="color:#000000;">In the terminal, type '''cargo run '''to see the output.</div>
+
height: u32,
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
}
| style="color:#000000;" |
+
|| <div style="color:#000000;">Next we will see about the declaring methods for '''struct'''.</div>
+
  
<div style="color:#000000;">Copy and paste the code from the code file.</div>
+
impl Rectangle {
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">struct Rectangle {</div>
+
  
<div style="color:#000000;">width: u32,</div>
+
fn area(&self) -> u32 {
  
<div style="color:#000000;">height: u32,</div>
+
self.width * self.height
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">impl Rectangle {</div>
+
}
  
<div style="color:#000000;">fn area(&self) -> u32 {</div>
+
fn main() {
  
<div style="color:#000000;">self.width * self.height</div>
+
let rect1 = Rectangle {
  
<div style="color:#000000;">}</div>
+
width: 30,
  
<div style="color:#000000;">}</div>
+
height: 50,
  
<div style="color:#000000;">fn main() {</div>
+
};
  
<div style="color:#000000;">let rect1 = Rectangle {</div>
+
println!(
  
<div style="color:#000000;">width: 30,</div>
+
"The area of the rectangle is {}",
  
<div style="color:#000000;">height: 50,</div>
+
rect1.area()
  
<div style="color:#000000;">};</div>
+
);
  
<div style="color:#000000;">println!(</div>
+
}
  
<div style="color:#000000;">"The area of the rectangle is {}",</div>
+
Press Ctrl+ S'''
 +
|| We define a '''struct''' named '''Rectangle''' with two fields:''' width and '''height''', both of type '''u32'''.
  
<div style="color:#000000;">rect1.area()</div>
+
We use an impl( '''implementation''')block to implement methods for the '''Rectangle struct'''.
  
<div style="color:#000000;">);</div>
+
Inside the '''implementation''' block, we define a method named area that calculates the area of the rectangle.
  
<div style="color:#000000;">} </div>
+
'''&self '''means it borrows the rectangle instance immutably.
  
<div style="color:#000000;">Press Ctrl+s</div>
+
'''self.width''' and '''self.height''' are the fields of the rectangle instance.
|| <div style="color:#000000;">We define a struct named Rectangle with two fields: width and height, both of type u32.</div>
+
  
<div style="color:#000000;">We use an impl( '''implementation''')block to implement methods for the Rectangle struct.</div>
+
It allows us to read width and height without taking ownership or modifying them.
  
<div style="color:#000000;">Inside the impl block, we define a method named area that calculates the area of the rectangle.</div>
+
Inside the '''main''' function, a new instance of '''Rectangle''' is created with, '''Width = 30 ''' and ''' height = 50'''.
  
<div style="color:#000000;">'''&self '''means it borrows the Rectangle instance immutably.</div>
 
  
<div style="color:#000000;">self.width and self.height are the fields of the rectangle instance.</div>
+
Save the Program.
 +
|-
 +
|| Type '''cargo run '''to see the output.
  
<div style="color:#000000;">It allows us to read width and height without taking ownership or modifying them.</div>
+
|| In the terminal, type '''cargo run '''to see the output.
  
<div style="color:#000000;">Inside the '''main''' function, a new instance of '''Rectangle''' is created with '''Width = 30 '''and''' height = 50'''</div>
+
It prints “The area of the rectangle is 1500”
 +
|-
 +
|| '''Slide 8'''
  
<div style="color:#000000;"></div>
+
'''Summary'''
  
<div style="color:#000000;"></div>
+
In this tutorial, we learn about
 +
|| This brings us to the end of this tutorial.
  
<div style="color:#000000;"></div>
+
Let us summarize.
 +
|-
 +
|| '''Slide 9'''
  
<div style="color:#000000;"></div>
+
'''Assignment'''
  
<div style="color:#000000;"></div>
+
|| As an Assignment, please do the following:
  
<div style="color:#000000;">Save the Program</div>
+
* Create a '''tuple''' named '''person''' with the following values:
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
* Print each value in the '''tuple''' using indexing
| style="color:#000000;" |
+
* Destructure the tuple into separate variables and print those variables.
|| <div style="color:#000000;">In the terminal, type '''cargo run '''to see the output.</div>
+
  
<div style="color:#000000;">It prints “The area of the rectangle is 1500”</div>
 
|- 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>
+
|-
 
+
|| '''Slide 10'''
<div style="color:#000000;">In this tutorial, we learn about</div><div style="color:#000000;margin-left:1.27cm;margin-right:0cm;"></div>
+
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
 
+
<div style="color:#000000;">Let us summarize. </div>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">'''Slide 8'''</div>
+
 
+
<div style="color:#000000;">'''Assignment'''</div>
+
 
+
|| <div style="color:#000000;">As an Assignment, please do the following:</div>
+
 
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Create a tuple named '''person''' with the following values:</div>
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Print each value in the tuple using indexing </div>
+
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Destructure the tuple into separate variables and print those variables.</div>
+
  
<div style="color:#000000;"></div>
+
''' Thank You'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| Thank you for joining.
| style="color:#000000;" | '''Slide 9 Thank You'''
+
| style="color:#000000;" | Thank you for joining.
+
 
|-
 
|-
 
|}
 
|}
<div style="color:#000000;margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Latest revision as of 11:48, 12 September 2025

Visual Cue Narration
Slide 1

Title Slide

Welcome to the Spoken Tutorial on Tuples and Struct in Rust.
Slide 2

Learning Objectives

In this tutorial, we will learn about:
  • Tuples and
  • Struct
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 a Rust program.
Slide 5

Code Files

  • 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

Tuples

  • Tuple is a compound data type
  • It is a group of values of different types, stored in a single variable.
  • Tuples have a fixed length - once declared they cannot grow or shrink in size.
Let us understand how tuples 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 tuplesdemo

In the menu bar, File >> Open folder

>> Desktop >> MyRustProject >> tuplesdemo

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

Type the command cargo new tuplesdemo 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 person: (f64, u8, bool) = (18000.00, 30, true);

println!("salary: {}", person.0);

println!("Age: {}", person.1);

println!("Is Active: {}", person.2);

// Destructuring a tuple

let(salary,age,status) =person;

println!("Destructured - Salary: {}, Age: {}, Status: {}", salary, age, status);

}

Press Ctrl+S

We have declared a variable called person with three different data types.

Here the tuple is declared with floating-point, integer and boolean data type.

We can access tuple elements using dot notation followed by the index of the element.

0 for the first element, 1 for the second, and so on.

Destructuring means it breaks the tuple into individual variables.

Here, salary becomes "18000", age becomes 30, and status becomes “true”.

It prints values stored in the new variables.

Save the program.

Click on terminal and select New Terminal.

In the terminal, type cargo run.

Point to the output.

Let us execute the code and see the output as shown,
Next let us see how to use Tuple as a return value.
Delete the existing code.

fn get_coordinates() -> (i32, i32) {

(10, 20)

}

fn main() {

let (x, y) = get_coordinates();

println!("X: {}, Y: {}", x, y);

}

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

This code shows that Tuples are commonly used to return multiple values from a function.

Save the file.

Point to the output. Check the output as shown.
Next we will see about Rust struct or structures.
Slide 7

Struct

  • Structs are similar to tuples
  • Structs can also hold multiple related values, of different data types.
Switch back to the Visual studio code editor.

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

struct User {

    active: bool,

    username: String,

    email: String,

}

fn main() {

    let user1 = User {

        active: true,

        username: String::from("someusername123"),

        email: String::from("someone@example.com"),

    };

println!(“{:?}”,user1.email);

}

Press Ctrl+S

Here’s an example of a struct.

We define a struct named User.

The fields with their names and data types are specified within the curly braces.

It groups together multiple pieces of data related to a user account.

user1 is a variable of type User.

User creates an instance of the User struct and assigns it to user1 variable.

We have the flexibility to assign key value pairs in any order, as struct is just a template.

To get a specific value from a struct, we use dot notation.

For example, to access this user’s email address, we use user1.email

Save the program.

In the terminal, type cargo run In the terminal, type cargo run to see the output.
Next we will see about the declaring methods for struct.

Copy and paste the code from the code file.

struct Rectangle {

width: u32,

height: u32,

}

impl Rectangle {

fn area(&self) -> u32 {

self.width * self.height

}

}

fn main() {

let rect1 = Rectangle {

width: 30,

height: 50,

};

println!(

"The area of the rectangle is {}",

rect1.area()

);

}

Press Ctrl+ S

We define a struct named Rectangle with two fields: width and height, both of type u32.

We use an impl( implementation)block to implement methods for the Rectangle struct.

Inside the implementation block, we define a method named area that calculates the area of the rectangle.

&self means it borrows the rectangle instance immutably.

self.width and self.height are the fields of the rectangle instance.

It allows us to read width and height without taking ownership or modifying them.

Inside the main function, a new instance of Rectangle is created with, Width = 30 and height = 50.


Save the Program.

Type cargo run to see the output. In the terminal, type cargo run to see the output.

It prints “The area of the rectangle is 1500”

Slide 8

Summary

In this tutorial, we learn about

This brings us to the end of this tutorial.

Let us summarize.

Slide 9

Assignment

As an Assignment, please do the following:
  • Create a tuple named person with the following values:
  • Print each value in the tuple using indexing
  • Destructure the tuple into separate variables and print those variables.


Slide 10

Thank You

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat