Data Types in Rust
In this lesson, you will learn about data types in Rust. Data types define what kind of values a variable can store and how the data is handled in memory.
Rust is a statically typed language, which means the type of every variable is known at compile time.
What Is a Data Type?
A data type specifies the kind of data a variable can hold, such as numbers, text, or logical values.
Knowing data types allows Rust to perform strict checks that improve safety, performance, and correctness.
Type Inference in Rust
In most cases, Rust can automatically determine the data type of a variable. This feature is called type inference.
Example:
fn main() {
let x = 10;
let y = 3.14;
println!("{}", x);
println!("{}", y);
}
Rust infers x as an integer and y as a floating-point number.
Explicit Type Annotation
You can also explicitly specify a variable’s type using a colon :.
Example:
fn main() {
let count: i32 = 50;
let price: f64 = 19.99;
println!("{}", count);
println!("{}", price);
}
Explicit types are useful when Rust cannot infer the type or when clarity is important.
Scalar Data Types
Scalar types represent a single value. Rust has four primary scalar data types:
- Integers
- Floating-point numbers
- Booleans
- Characters
Integer Types
Integer types store whole numbers. Rust provides signed and unsigned integers of various sizes.
i8,i16,i32,i64,i128u8,u16,u32,u64,u128
Example:
fn main() {
let a: i32 = -25;
let b: u32 = 100;
println!("{}", a);
println!("{}", b);
}
Floating-Point Types
Floating-point types store decimal values. Rust provides two floating-point types:
f32– single precisionf64– double precision (default)
fn main() {
let pi: f64 = 3.14159;
let rate: f32 = 2.5;
println!("{}", pi);
println!("{}", rate);
}
Boolean Type
The boolean type represents logical values:
true or false.
fn main() {
let is_active: bool = true;
println!("{}", is_active);
}
Booleans are commonly used in conditions and control flow.
Character Type
The char type stores a single Unicode character.
Characters are written using single quotes.
fn main() {
let letter: char = 'R';
let symbol: char = '✓';
println!("{}", letter);
println!("{}", symbol);
}
Rust’s char type can store Unicode characters, not just ASCII.
Compound Data Types
Compound types group multiple values into a single type. The two main compound types in Rust are:
- Tuples
- Arrays
Tuples
A tuple groups values of different types into one compound value.
fn main() {
let person = ("Alice", 30, true);
println!("{}", person.0);
println!("{}", person.1);
}
Tuple values are accessed using index notation.
Arrays
Arrays store multiple values of the same type. Their size is fixed and known at compile time.
fn main() {
let numbers = [1, 2, 3, 4, 5];
println!("{}", numbers[0]);
}
Arrays provide fast and safe access to elements.
📝 Practice Exercises
Exercise 1
Create an integer variable and print its value.
Exercise 2
Create a floating-point variable and print it.
Exercise 3
Create a tuple with three different data types.
Exercise 4
Create an array of five numbers and access one element.
✅ Practice Answers
Answer 1
fn main() {
let value: i32 = 42;
println!("{}", value);
}
Answer 2
fn main() {
let price: f64 = 9.99;
println!("{}", price);
}
Answer 3
fn main() {
let data = ("Rust", 2025, true);
println!("{}", data.0);
}
Answer 4
fn main() {
let nums = [10, 20, 30, 40, 50];
println!("{}", nums[2]);
}
What’s Next?
Now that you understand Rust’s data types, you are ready to learn how strings work in Rust.
In the next lesson, you will explore string literals and the String type in detail.