Skip to main content

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

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.