Rust Data Types
🦀 Rust Data Types
🔹 1. Scalar Data Types
Scalar types represent single values.
▶️ Integer Types
| Type | Size | Example |
|---|---|---|
i8 / u8 |
8-bit | let a: i8 = -10; |
i16 / u16 |
16-bit | |
i32 / u32 |
32-bit (default) | let b = 20; |
i64 / u64 |
64-bit | |
i128 / u128 |
128-bit | |
isize / usize |
arch dependent |
▶️ Floating-Point Types
| Type | Size |
|---|---|
f32 |
32-bit |
f64 |
64-bit (default) |
▶️ Boolean Type
▶️ Character Type
✔ Rust char is 4 bytes (Unicode).
🔹 2. Compound Data Types
Compound types group multiple values into one type.
▶️ Tuple
Fixed-size collection of different data types.
Destructuring:
▶️ Array
Fixed-size collection of same data type.
🔹 3. String Types (Very Important)
▶️ &str (String Slice)
-
Immutable
-
Stored in read-only memory
▶️ String (Heap Allocated)
🔹 4. Custom Data Types
▶️ Struct
▶️ Enum
🔹 5. Option Type (No Null in Rust)
Rust has no null. Instead:
🔹 6. Result Type (Error Handling)
🔑 Type Inference Example
🧠 Important Notes
-
Rust prevents invalid memory access
-
No implicit type conversion
-
Use
asfor explicit casting -
Safer than C/C++ data handling
