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 = 'πŸ¦€';
                                    
Data Type Categories

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

🧠 Test Your Knowledge

Which is the default integer type in Rust?