22/04/2026 07:11am

Basic Data Types: Exploring Scalar and Compound Types | Rust The Series EP.5
#Rust Programming
#Rust
#Data Types
#Scalar types
#Compound types
#Rust tutorial
#Backend Development
#Superdev Academy
In EP.4, we learned how to declare variables and manage mutability. However, variables always go hand-in-hand with "Data Types." In Rust, every value must have a specific type to allow the compiler to ensure the program runs safely and efficiently.
Rust is a statically typed language, meaning it must know the types of all variables at compile time. We can categorize Rust data types into two main groups: Scalar (single values) and Compound (grouped values).
1. Scalar Types
A scalar type represents a single value. Rust has four primary scalar types:
🔢 Integers
Integers are numbers without a fractional component. Rust provides various sizes based on memory (bits), divided into Signed (i) and Unsigned (u):
Length | Signed | Unsigned |
8-bit |
|
|
32-bit |
|
|
64-bit |
|
|
arch |
|
|
Note:
isizeandusizedepend on the architecture of the computer your program is running on (e.g., 64-bit on a Mac M4). These are primarily used when indexing collections like Arrays.
🥧 Floating-Point Types
Rust has two primitive types for numbers with decimal points: f32 and f64 (the default).
Rust
let x = 2.0; // f64 by default
let y: f32 = 3.0; // explicit f32 declaration
✅ Booleans
Booleans have two possible values: true and false. They are 1 byte in size.
Rust
let is_active = true;
let is_finished: bool = false;
🔤 Characters
In Rust, the char type is specified with single quotes (') and is 4 bytes in size. Why? Because it represents a Unicode Scalar Value, meaning it can store much more than just ASCII—it supports Emojis, Thai characters, and more!
Rust
let c = 'z';
let heart_eye_emoji = '😻';
2. Compound Types
Compound types can group multiple values into one type. Rust has two primitive compound types:
📦 Tuples
A tuple is a general way of grouping together a number of values with a variety of types into one compound type. Tuples have a fixed length: once declared, they cannot grow or shrink.
Rust
let person: (&str, i32, bool) = ("Ploy", 25, true);
// Destructuring to get values
let (name, age, status) = person;
// Accessing via Index (Dot notation)
let name = person.0;
🔢 Arrays
Unlike a tuple, every element of an array must have the same type. Arrays in Rust have a fixed length and are allocated on the stack, making them incredibly fast.
Rust
let months = ["Jan", "Feb", "Mar"];
let numbers: [i32; 5] = [1, 2, 3, 4, 5]; // [Type; Length]
// Accessing elements
let first = numbers[0];
Warning: If you try to access an index that doesn't exist (Out of bounds), Rust will cause the program to "Panic" (exit immediately) to prevent memory insecurity.
Summary
Choosing the right type not only saves memory but also allows the Rust compiler to act as your "assistant," verifying the logic and correctness of your code.
In EP.6, get ready for Control Flow! We will explore how to use if-else and various types of loops to make our programs handle complex conditions.
🎯 Stay Tuned with Superdev Academy:
Don't miss out on deep-dive technical articles and updates. Follow us on all platforms:
🔵 Facebook: Superdev Academy Thailand
🎬 YouTube: Superdev Academy Channel
📸 Instagram: @superdevacademy
🎬 TikTok: @superdevacademy
🌐 Website: superdevacademy.com