Writing / Detail

Primitive Types: The "Core Building Blocks"

2026.04.07
Technology
2672 Words
- Views
- Comments

In JavaScript, you have one “number” type and one “string” type. In Rust, you have Scalar and Compound primitives that define the physical shape and size of your data in memory.

Think of Rust Primitives as a Set of Specialized Tools—each one optimized for a specific memory job, unlike JS’s multi-purpose “everything is an object” approach.


🏗️ The 3-Step Narrative

1. Scalar Types: The Individual Tools

A scalar type represents a single value. Rust has four primary scalar types:

  • Integers: Signed (i) and Unsigned (u). From 8-bit (i8) to 128-bit (i128).
  • Floating-Point: For numbers with decimals (f32, f64).
  • Booleans: true or false.
  • Characters: char represents a single Unicode scalar value (e.g., 'z', '😻', or 'ℤ').

IMPORTANT: Signed vs. Unsigned

Use Signed (i) numbers if they could be negative. Use Unsigned (u) for things that must be positive (like a “count” or “index”).

2. Compound Types: The Toolboxes

These group multiple values into a single “container.”

  • Tuples: Grouping values of different types. Great for returning multiple results from a single function.
    let tup: (i32, f64, u8) = (500, 6.4, 1);
    let (x, y, z) = tup; // 🪄 Destructuring! Just like in JS.
  • Arrays: Grouping values of the same type with a fixed length.
    let a = [1, 2, 3, 4, 5]; // ⬅️ Stays on the stack!
    let first = a[0]; // ⬅️ Accessing by index

3. The Contract: Fixed Size

The “Aha!” moment: In JS, an array can grow from 1 to 100 elements. In Rust, an Array has a fixed size defined at compile-time. If you need it to grow, you must use a Collections (Vec & HashMaps).


🔬 From the Official Book

Unicode Characters (char)

Unlike JS strings where characters are usually 16-bit, a Rust char is 4 bytes in size and represents a Unicode Scalar Value. This means it handles emojis and international characters perfectly by default.

let smiley = '😊'; // ✅ A valid char

💡 What Primitive Types Unlock

  • Memory Efficiency: By using u8 instead of a 64-bit float for small numbers, your program consumes significantly less RAM.
  • Predictable Precision: Choosing exactly between f32 and f64 gives you control over the mathematical precision of your application.
  • Compile-Time Safety: Rust’s strict typing ensures that you can’t accidentally pass a floating-point number into an integer-based index.
  • Speed: Scalar types are stored directly on the stack, allowing for near-instant access compared to heap-based objects.