Rust-Programming-Language/C2/Tuples-and-Struct/English

From Script | Spoken-Tutorial
Revision as of 12:03, 11 September 2025 by Madhurig (Talk | contribs)

Jump to: navigation, search
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.

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 impl 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

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