Rust-Programming-Language/C2/Data-types/English

From Script | Spoken-Tutorial
Revision as of 15:12, 25 July 2025 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide:
Title Slide
Welcome to the Spoken Tutorial on Data types in Rust.
Slide 2

Learning Objective

In this tutorial, we will learn about:
  • Data types
Slide 3

System Requirements

This tutorial is recorded using
  • Ubuntu Linux OS version 22.04
  • Rust 1.80.0
  • Visual Studio Code 1.19.0
Slide 4 To follow this tutorial,
  • You should be familiar with compiling and running a Rust program
Slide 5

Code Files

  • The following code files are required to practise this tutorial
  • These files are provided in the code files link of this tutorial page

We will see about various data types in Rust.
Slide:

Rust - Data types

  • Every value in Rust is of a certain Type.
  • Rust is a statically typed language that checks the types of variables at compile time.
  • Compiler must know the type of all variables at compile time
  • This helps the compiler to optimize for efficient and faster runtime execution.
Slide:
Scalar Type
Data types are divided as
  • Scalar and Compound Types

A Scalar Type is referred to as a single value. Rust has four Scalar Data types. They are

  • Integers
  • Float
  • Boolean
  • Character
Open Visual Studio Code editor Let us open the Visual Studio Code editor.
Open the terminal by pressing
Ctrl+Tilde (~)

Click on Terminal and select New Terminal.

In the menu bar, click on Terminal and select New Terminal.

We can see a terminal window at the bottom.

Type at the prompt:

cargo new datatypes

Go to our working directory MyRustProject as explained earlier.

Type the command cargo new datatypes and press Enter

Open the created project as shown.

Point to main.rs file. In the main.rs file, copy and paste the code from the codefile.
Highlight according to narration
fn main() {
let x: i32 = 100;
let y: u32 = 200;
let f: f64 = 3.14;
let flag: bool = true;
let character: char = 'R';
println!("Integer: {}", x);
println!("Integer: {}", y);
println!("Float: {}", f);
println!("Boolean: {}", flag);
println!("Character: {}", character);
}
We use integer data types to store whole numbers.
i specifies signed integer type i.e(it can store both positive or negative value)
32 is the size of the data type i.e (it takes 32 bits of space in memory)
Integers can be of many types as i8, i16, i32, i64.
u specifies the unsigned integer type i.e (it can only store positive integer values.)
If we try to store negative numbers to u32 type variables, we will get an error.
Here, the f character represents a floating point number.
32 and 64 represent the size in bits.
A boolean data type can have two possible values: true or false.
char represents the character type variable and we use single quotes to represent a character.
We can also store special characters like $, & etc. using the character type.
Save the program.
Let us compile and run the program and see the output.
In the terminal, type cargo run.

We can see the various data types printed as output.

Slide:

Type Inference in Rust

  • In Rust we can create variables without mentioning a data type
  • let x=50;
  • Rust will automatically set i32 as default type for integer by looking at the value 50.
  • This process is Type Inference.
Slide:

Constants

const PI: f32 = 3.14159265358979323846;
fn main() {
let radius = 5.0;
let circumference = 2.0 * PI * radius;
println!("The circumference of a circle with radius {} is {}", radius, circumference);
}
This is an example for constants.
  • Constants are variables that are immutable and have a fixed value.
  • They are declared using the const keyword.
  • You must declare the type of the value.
This brings us to the end of this tutorial.

Let us summarize.

Slide:

Summary

In this tutorial, we learnt about
  • Data types


Slide:

Assignment

fn main() { let y: u32 = - 200; println!("Integer: {}", y); }

As an assignment, do the following.
  • Run the above program
  • Analyse the error and correct the program.
Thank You

We would like to thank Vishal Pokuri from VIT Vellore for content contribution.

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat