Rust Data Types
Understanding different types of data in Rust
π― What are Rust Data Types?
Data types in Rust specify what kind of data a variable can store. Rust is statically typed, meaning all variable types must be known at compile time for memory safety and performance.
// Different data types in Rust
let integer: i32 = 42;
let floating: f64 = 3.14;
let boolean: bool = true;
let character: char = 'π¦';
Rust Data Type Categories
Integer Types
Whole numbers without decimal points
let age: i32 = 25;
let count: u64 = 1000;
Floating Point
Numbers with decimal points
let price: f32 = 19.99;
let pi: f64 = 3.14159;
Boolean
True or false values
let is_active: bool = true;
let is_complete = false;
Character & String
Text and individual characters
let letter: char = 'A';
let name: &str = "Rust";
πΉ Integer Types
Rust has several integer types with different sizes:
fn main() {
// Signed integers (can be negative)
let small: i8 = -128; // -128 to 127
let medium: i16 = -32000; // -32,768 to 32,767
let normal: i32 = -2000000; // Default integer type
let large: i64 = -9000000000;
// Unsigned integers (only positive)
let tiny: u8 = 255; // 0 to 255
let big: u32 = 4000000; // 0 to 4,294,967,295
println!("Signed: {}, {}, {}, {}", small, medium, normal, large);
println!("Unsigned: {}, {}", tiny, big);
}
Output:
Signed: -128, -32000, -2000000, -9000000000
Unsigned: 255, 4000000
πΉ Floating Point Types
Rust has two floating point types for decimal numbers:
fn main() {
// f32 - single precision (32 bits)
let temperature: f32 = 23.5;
// f64 - double precision (64 bits) - default
let precise_pi = 3.141592653589793;
// Mathematical operations
let sum = 2.5 + 3.7;
let product = 4.0 * 2.5;
println!("Temperature: {}Β°C", temperature);
println!("Pi: {}", precise_pi);
println!("Sum: {}, Product: {}", sum, product);
}
Output:
Temperature: 23.5Β°C
Pi: 3.141592653589793
Sum: 6.2, Product: 10
πΉ Boolean and Character Types
Boolean for true/false values and char for single characters:
fn main() {
// Boolean type
let is_sunny: bool = true;
let is_raining = false;
// Character type (Unicode)
let letter: char = 'R';
let emoji: char = 'π¦';
let chinese: char = 'δΈ';
println!("Sunny: {}, Raining: {}", is_sunny, is_raining);
println!("Letter: {}, Emoji: {}, Chinese: {}", letter, emoji, chinese);
// Boolean operations
let nice_weather = is_sunny && !is_raining;
println!("Nice weather: {}", nice_weather);
}
Output:
Sunny: true, Raining: false
Letter: R, Emoji: π¦, Chinese: δΈ
Nice weather: true
πΉ String Types
Rust has different ways to handle text:
fn main() {
// String slice (&str) - immutable
let greeting: &str = "Hello, World!";
// String - mutable, owned
let mut name = String::from("Alice");
name.push_str(" Smith");
// String literals
let language = "Rust";
println!("{}", greeting);
println!("Name: {}", name);
println!("Language: {}", language);
// String length
println!("Greeting length: {}", greeting.len());
}
Output:
Hello, World!
Name: Alice Smith
Language: Rust
Greeting length: 13