Data Types
Rust’s type system ensures safety and efficiency, making it a powerful language for systems programming. This tutorial explores fundamental data types, compound types, and custom data types in detail.
Fundamental Data Types
Fundamental data types in Rust are divided into scalar and compound types.
Scalar Types
Scalar types represent a single value.
1. Unsigned Integers
• Definition: Represent non-negative whole numbers.
• Types: u8, u16, u32, u64, u128, usize
• usize is platform-dependent (32-bit on a 32-bit system, 64-bit on a 64-bit system).
Example:
let small: u8 = 255; // Maximum value for u8
let large: u64 = 1_000_000; // Underscores improve readability
println!("Small: {}, Large: {}", small, large);
2. Signed Integers
• Definition: Represent whole numbers, including negatives.
• Types: i8, i16, i32, i64, i128, isize
•isize is platform-dependent.
Example:
let positive: i32 = 42;
let negative: i32 = -42;
println!("Positive: {}, Negative: {}", positive, negative);
3. Floating-Point Numbers
• Definition: Represent decimal numbers.
• Types: f32 (single precision), f64 (double precision, default).
Example:
let pi: f64 = 3.14159;
let e: f32 = 2.718;
println!("Pi: {}, e: {}", pi, e);
4. Boolean
• Definition: Represents a true or false value.
• Type: bool
Example:
let is_rust_fun: bool = true;
println!("Rust is fun: {}", is_rust_fun);
5. Character
• Definition: Represents a single Unicode character.
• Type: char
Example:
let letter: char = 'A';
let emoji: char = '🚀'; // Rocket emoji
println!("Letter: {}", letter);
println!("Emoji: {}", emoji);
Platform-Specific Integers
• Definition: Integer types dependent on system architecture.
• Types: usize, isize
Example:
let size: usize = 10;
let offset: isize = -5;
println!("Size: {}, Offset: {}", size, offset);
Compound Types
Compound types group multiple values into a single type. Rust has two compound types: tuples and arrays.
1. Tuples
A tuple is a fixed-size collection of values of different types.
Example:
let person: (&str, i32, f64) = ("saifan", 20, 65.5);
println!("Name: {}, Age: {}, Weight: {}", person.0, person.1, person.2);
2. Arrays
An array is a fixed-size collection of elements of the same type.
Example:
let numbers: [i32; 3] = [1, 2, 3];
println!("First number: {}", numbers[0]);
3. Slices
Slices provide a view into a contiguous sequence of elements in an array or vector.
Example:
let numbers: [i32; 4] = [10, 20, 30, 40];
let slice: &[i32] = &numbers[1..3];
println!("Slice: {:?}", slice);
Custom Data Types
Rust allows you to define your own data types using structs, enums, and type aliases.
Structs
Structs group related data together. They can be:
1. Named fields
2. Tuple structs
3. Unit-like structs
Example:
struct Person {
name: String,
age: u8,
}
fn main() {
let person = Person {
name: String::from("Bob"),
age: 25,
};
println!("Name: {}, Age: {}", person.name, person.age);
}
Enums
Enums represent a type that can have different values, known as variants.
Example:
enum Direction {
North,
South,
East,
West,
}
fn main(){
let dir = Direction::North;
match dir {
Direction::North => println!("Heading North"),
_ => println!("Other direction"),
}
}
Type Aliases
Type aliases give a new name to an existing type.
Example
type Kilometers = i32;
fn main() {
let distance: Kilometers = 100;
println!("Distance: {} km", distance);
}
Conclusion
Rust’s data types provide a rich and flexible foundation for writing efficient, safe, and expressive code. Understanding these types helps you make better design decisions and write more reliable programs.