Skip to main content

Rust Data Types

Ayesha
EditReport

Rust Data Types

Data types define the kind of value a variable can store.

Rust is a statically typed language, which means the compiler knows the type of every variable at compile time.

let age = 20;
let price = 99.99;
let is_active = true;

Rust mainly provides two categories of data types:

  1. Scalar Data Types
  2. Compound Data Types

Video Explanationโ€‹


1. Scalar Data Types

Scalar data types store a single value.

Rust provides four main scalar types:

  • Integer
  • Floating Point
  • Boolean
  • Character

Integer Types

Integer types are used to store whole numbers.

Rust supports both signed and unsigned integers.

  • Signed integers can store positive and negative values.
  • Unsigned integers store only positive values.
TypeSizeExample
i88-bit-10
u88-bit10
i1616-bit-200
u1616-bit200
i3232-bit-500
u3232-bit500
i6464-bit-9000
u6464-bit9000
i128128-bit-100000
u128128-bit100000
isizearch-dependent-1
usizearch-dependent1

i32 is the default integer type in Rust.

Exampleโ€‹

fn main() {
let age: u8 = 20;
let temperature: i32 = -15;

println!("Age: {}", age);
println!("Temperature: {}", temperature);
}

Outputโ€‹

Age: 20
Temperature: -15

Floating Point Types

Floating point types are used to store decimal values.

Rust provides two floating point types:

TypeSizeExample
f3232-bit3.14
f6464-bit99.99

f64 is the default floating point type.

Exampleโ€‹

fn main() {
let pi: f32 = 3.14;
let price: f64 = 99.99;

println!("Pi: {}", pi);
println!("Price: {}", price);
}

Outputโ€‹

Pi: 3.14
Price: 99.99

Boolean Type

Boolean type stores only two values:

  • true
  • false

Booleans are mostly used in conditions and decision making.

Exampleโ€‹

fn main() {
let is_rust_easy: bool = true;
let is_sky_green: bool = false;

println!("{}", is_rust_easy);
println!("{}", is_sky_green);
}

Outputโ€‹

true
false

Character Type

The char type stores a single character.

Character values are written inside single quotes.

Exampleโ€‹

fn main() {
let grade: char = 'A';
let symbol: char = '#';

println!("{}", grade);
println!("{}", symbol);
}

Outputโ€‹

A
#

2. Compound Data Types

Compound data types can store multiple values together.

Rust mainly provides:

  • Tuples
  • Arrays

Tuples

A tuple can store multiple values of different data types.

Tuple values are written inside parentheses.

Exampleโ€‹

fn main() {
let student = ("Hemlata", 21, 8.5);

println!("Name: {}", student.0);
println!("Age: {}", student.1);
println!("CGPA: {}", student.2);
}

Outputโ€‹

Name: Hemlata
Age: 21
CGPA: 8.5

Tuple Destructuringโ€‹

Rust allows unpacking tuple values into variables.

fn main() {
let person = ("Alice", 25);

let (name, age) = person;

println!("Name: {}", name);
println!("Age: {}", age);
}

Arrays

Arrays store multiple values of the same data type.

Arrays in Rust have fixed size.

Syntaxโ€‹

let numbers = [10, 20, 30, 40];

Exampleโ€‹

fn main() {
let numbers = [10, 20, 30, 40, 50];

println!("First Element: {}", numbers[0]);
println!("Second Element: {}", numbers[1]);
}

Outputโ€‹

First Element: 10
Second Element: 20

Array with Data Typeโ€‹

fn main() {
let marks: [i32; 5] = [90, 85, 88, 92, 95];

// Use the debug formatter {:?} to print the array
println!("{:?}", marks);
}

String Slice (&str)

String slices are immutable string references.

They are usually used for fixed text.

Exampleโ€‹

fn main() {
let language: &str = "Rust";

println!("{}", language);
}

Outputโ€‹

Rust

String

String is a growable and mutable string type.

Exampleโ€‹

fn main() {
let mut name = String::from("Rust");

name.push_str(" Language");

println!("{}", name);
}

Outputโ€‹

Rust Language

Type Inference

Rust can automatically detect the data type of variables.

This feature is called type inference.

Exampleโ€‹

fn main() {
let number = 100;
let pi = 3.14;
let status = true;

println!("{}", number);
println!("{}", pi);
println!("{}", status);
}

In this example:

  • number becomes i32
  • pi becomes f64
  • status becomes bool

Type Casting

Rust does not automatically convert one data type into another.

We use the as keyword for type conversion.

Exampleโ€‹

fn main() {
let num1: i32 = 10;
let num2: f64 = num1 as f64;

println!("{}", num1);
println!("{}", num2);
}

Outputโ€‹

10
10

Constants in Rust

Constants are values that cannot be changed during program execution.

Constants are declared using the const keyword.

Exampleโ€‹

const PI: f64 = 3.14159;

fn main() {
println!("{}", PI);
}

Mutability in Rust

Variables are immutable by default in Rust.

Use the mut keyword to make variables mutable.

Exampleโ€‹

fn main() {
let mut score = 50;

score = 80;

println!("{}", score);
}

Outputโ€‹

80

Summary

Rust provides different data types to store different kinds of values safely and efficiently.

Scalar Data Typesโ€‹

  • Integer
  • Floating Point
  • Boolean
  • Character

Compound Data Typesโ€‹

  • Tuples
  • Arrays

Rust also supports:

  • Strings
  • Type Inference
  • Type Casting
  • Constants
  • Mutable Variables

These features make Rust powerful, safe, and fast for system programming and application development.

Track Your Progress

Done with this topic? Mark it as complete to track your progress.