What Are Rust Data Types?

Every value has a specific data type in Rust programming language, which tells the compiler what kind of value is stored and how it can be used. Rust is a statically typed language, not working like Python or JavaScript.

Rust data types are checked at compile time and ensure your code is safer, faster, and without errors because it can detect type mismatches before the program runs

Why Are Data Types Important In Rust?

  • This programming language optimises memory usage and performance. For example, exact sizes for variables.
  • Ensures operations and prevents bugs. You can’t accidentally add a string to a number.

Categories of Data Types In Rust

It has two main data type categories:

  1. Primitive (Scalar) Types
  2. Compound Types

1) Primitive (Scaler) Data Types

These data types contain multiple types like integers, floating-point numbers, Boolean, and Character (char)

a) Integers

Integers include the whole numbers, positive and negative. Rust supports signed (i) and unsigned (u) integers with different sizes like 8, 16, 32, 64, and 128.

LengthSigned (i)Unsigned (u)
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

Simple example of 32bit and 64bit integer:

fn main() {
let age: i32 = 25; // 32-bit signed integer
let population: u64 = 7_800_000_000; // 64-bit unsigned integer
println!("Age: {}, World Population: {}", age, population);
}

Note: i32 is the default type in Rust, and use u types when you only need positive numbers.

b) Floating-Point Numbers

Rust has two types for decimal numbers, and 64-bit float is the default size number in floating point.

  • f32 (32-bit float)
  • f64 (64-bit float, default)

For example:

fn main() {
let temperature: f64 = 36.6;
let rating: f32 = 4.5;
println!("Temperature: {}, Rating: {}", temperature, rating);
}

c) Boolean

Boolean represents the true or false events. For example:

fn main() {
let is_rust_easy: bool = true;
println!("Is Rust easy to learn? {}", is_rust_easy);
}

d) Character (char)

It represents a single Unicode character.

fn main() {
let letter: char = 'R';
let emoji: char = '😊';
println!("Letter: {}, Emoji: {}", letter, emoji);
}

2) Compound Data Types

a) Tuples

A tuple can hold different types multiple values in a single variable.

fn main() {
let user: (&str, i32, bool) = ("Alice", 25, true);
println!("Name: {}, Age: {}, Active: {}", user.0, user.1, user.2);
}

We can access tuple elements using a dot( . ) with an index. This method is useful for grouping values without creating a struct.

b) Arrays

Rust arrays have a fixed length instead of Python lists. For example:

fn main() {
let scores: [i32; 4] = [85, 90, 78, 92]; // Array of 4 integers
println!("First score: {}", scores[0]);
println!("Total subjects: {}", scores.len());
}

See this code carefully. The type [i32; 4] means “an array of 4 integers”.

Simple Example of Multiple Data Types

We will use the different types together to calculate a discount for a user:

fn main() {
let username: &str = "Bob"; // String slice
let items_bought: i32 = 5; // Integer
let price_per_item: f64 = 20.0; // Float
let has_discount: bool = true; // Boolean

let total = price_per_item * items_bought as f64;

// Apply 10% discount if eligible (shadowing used)
let total = if has_discount { total * 0.9 } else { total };

println!("Customer: {}", username);
println!("Items: {}, Price per item: ${}", items_bought, price_per_item);
println!("Final Bill: ${:.2}", total);
}

Output of this program:

Customer: Bob
Items: 5, Price per item: $20
Final Bill: $90.00

Your Exercises of Data Types

  1. Create a program that uses a tuple to store and display details about a book.
  2. Write a program that creates an array of integers and finds their sum.

Leave a Comment