🦀 Rust Primitive Data Types: The Building Blocks of Your Code
Imagine you have a toy box. Inside, you keep different kinds of toys:
- Counting blocks for numbers
- Light switches for yes/no
- Letter stamps for writing
In Rust, we have similar building blocks called primitive data types. They’re the simplest, most basic pieces we use to build everything else!
🔢 Integer Types: Counting Without Decimals
Think of integers like counting your fingers. You can count 1, 2, 3… but never 1.5 fingers!
Two Flavors: Signed and Unsigned
Signed integers can be positive OR negative (like temperature: -5°C or +20°C) Unsigned integers are ONLY positive (like counting toys: 0, 1, 2, 3…)
| Type | Size | Range (Signed) | Range (Unsigned) |
|---|---|---|---|
i8 / u8 |
8 bits | -128 to 127 | 0 to 255 |
i16 / u16 |
16 bits | -32,768 to 32,767 | 0 to 65,535 |
i32 / u32 |
32 bits | ±2 billion | 0 to 4 billion |
i64 / u64 |
64 bits | ±9 quintillion | 0 to 18 quintillion |
i128 / u128 |
128 bits | HUGE! | EVEN BIGGER! |
isize / usize |
Depends | Computer’s word size | Computer’s word size |
Simple Example:
let age: u8 = 10; // Age is always positive
let temperature: i8 = -5; // Temperature can be negative
let big_number: i32 = 1_000_000; // Underscores help readability!
Why different sizes?
- Small boxes (
i8) use less memory but hold less - Big boxes (
i64) hold more but use more memory
Pick the right size box for what you’re storing!
🎈 Floating-Point Types: Numbers with Decimal Points
What if you want to say “I ate 2.5 cookies”? You need floating-point numbers!
Rust has two types:
f32— Single precision (32 bits) — good for gamesf64— Double precision (64 bits) — more accurate (default)
let pi: f64 = 3.14159;
let price: f32 = 19.99;
let half: f64 = 0.5;
Why “Floating”?
The decimal point can “float” around:
3.14(point after 3)0.0314(point moved left)314.0(point moved right)
Default is f64 because modern computers handle it fast and it’s more precise!
💡 Boolean Type: Yes or No, True or False
The simplest type! Like a light switch — only two positions:
true— ON, yes, correctfalse— OFF, no, wrong
let is_sunny: bool = true;
let is_raining: bool = false;
let rust_is_fun: bool = true;
Booleans are used for decisions:
let is_adult = age >= 18;
// If age is 10, is_adult becomes false
// If age is 25, is_adult becomes true
🔤 Character Type: One Single Letter
A char holds ONE character — like a single letter stamp!
let letter: char = 'A';
let emoji: char = '🦀';
let heart: char = '❤';
let number_char: char = '7';
Important Rules:
- Use single quotes
'A'(not double quotes) - One character only
- Uses 4 bytes (supports ALL Unicode characters, including emojis!)
String vs Char:
'A'is a char (one character)"A"is a string (text, even if just one letter)
🏷️ Numeric Literals and Suffixes
Literals are the actual numbers you type in your code.
Adding Type Suffixes
You can tell Rust exactly what type by adding a suffix:
let a = 42i32; // i32 integer
let b = 42u8; // u8 unsigned
let c = 3.14f32; // f32 float
let d = 100_000i64; // i64 with underscore
Different Number Formats
Rust lets you write numbers in fun ways:
let decimal = 98_222; // Decimal (normal)
let hex = 0xff; // Hexadecimal (starts with 0x)
let octal = 0o77; // Octal (starts with 0o)
let binary = 0b1111_0000; // Binary (starts with 0b)
let byte = b'A'; // Byte (u8 only, ASCII)
Underscore Magic ✨
Underscores make big numbers readable:
let million = 1_000_000; // Easy to read!
let binary = 0b1111_0000; // Groups of 4 bits
let phone = 555_123_4567; // Like real phone numbers
🎯 Quick Summary
graph TD A["Rust Primitive Types"] --> B["Numbers"] A --> C["Boolean"] A --> D["Character"] B --> E["Integers"] B --> F["Floats"] E --> G["Signed: i8, i16, i32, i64, i128"] E --> H["Unsigned: u8, u16, u32, u64, u128"] F --> I["f32 - Single precision"] F --> J["f64 - Double precision"] C --> K["true / false"] D --> L["Single Unicode char"]
🚀 Why This Matters
These primitives are like LEGO bricks:
- Integers count things
- Floats measure things
- Booleans decide things
- Characters spell things
Everything in Rust is built from these simple pieces!
Remember:
- Use
i32for most whole numbers (Rust’s default) - Use
f64for decimals (Rust’s default) - Use
boolfor true/false decisions - Use
charfor single characters with single quotes
Now you have all the building blocks to start your Rust adventure! 🦀
