Rust - Data Types
In Rust, data types are an essential part of the language, allowing you to define what kind of data a variable can hold. Rust is statically typed, meaning that every variable must have a type known at compile time. Rust has two main categories of data types: scalar and compound.
Scalar Types
Scalar types represent a single value. Rust has four primary scalar types:
- Integers
- Floating-point numbers
- Booleans
- Characters
1. Integers
Integers are whole numbers. Rust supports both signed and unsigned integers of various sizes.
- Signed integers:
i8
,i16
,i32
,i64
,i128
,isize
- Unsigned integers:
u8
,u16
,u32
,u64
,u128
,usize
Examples:
rustfn main() {
let x: i32 = -10; // Signed 32-bit integer
let y: u32 = 100; // Unsigned 32-bit integer
println!("x: {}, y: {}", x, y);
}
2. Floating-point Numbers
Floating-point numbers are numbers with decimal points. Rust supports two types:
f32
: 32-bit floating pointf64
: 64-bit floating point (default)
Examples:
rustfn main() {
let x: f32 = 3.14;
let y: f64 = 2.718;
println!("x: {}, y: {}", x, y);
}
3. Booleans
Booleans represent a value that can be either true
or false
.
Examples:
rustfn main() {
let t: bool = true;
let f: bool = false;
println!("t: {}, f: {}", t, f);
}
4. Characters
Characters represent a single Unicode scalar value and are denoted with single quotes.
Examples:
rustfn main() {
let c: char = 'z';
let heart_eyed_cat: char = '?';
println!("c: {}, heart_eyed_cat: {}", c, heart_eyed_cat);
}
Compound Types
Compound types can group multiple values into one type. Rust has two primary compound types:
- Tuples
- Arrays
1. Tuples
A tuple is a fixed-length group of values of possibly different types.
Examples:
rustfn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup; // Destructuring the tuple
println!("x: {}, y: {}, z: {}", x, y, z);
// Accessing tuple elements directly
println!("tup.0: {}, tup.1: {}, tup.2: {}", tup.0, tup.1, tup.2);
}
2. Arrays
An array is a fixed-length group of values of the same type.
Examples:
rustfn main() {
let arr: [i32; 5] = [1, 2, 3, 4, 5];
println!("arr: {:?}", arr);
// Accessing array elements
println!("arr[0]: {}, arr[4]: {}", arr[0], arr[4]);
// Arrays with the same value
let repeated_array = [3; 5]; // [3, 3, 3, 3, 3]
println!("repeated_array: {:?}", repeated_array);
}
Additional Examples
Here are some additional examples to demonstrate the usage of different data types:
rustfn main() {
// Integer
let x: i32 = 42;
// Floating-point
let y: f64 = 3.14;
// Boolean
let z: bool = true;
// Character
let letter: char = 'A';
// Tuple
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (a, b, c) = tup;
// Array
let arr: [i32; 4] = [1, 2, 3, 4];
println!("x: {}, y: {}, z: {}, letter: {}", x, y, z, letter);
println!("Tuple - a: {}, b: {}, c: {}", a, b, c);
println!("Array - arr[0]: {}, arr[3]: {}", arr[0], arr[3]);
}
Summary
- Scalar Types: Integers, Floating-point numbers, Booleans, Characters
- Compound Types: Tuples, Arrays
Understanding Rust’s data types is fundamental to writing robust and efficient Rust programs. Each type has its specific use cases and benefits, enabling Rust to handle a wide variety of programming tasks efficiently.