In this lesson, you will learn how to represent basic, indivisible units of data using Rust’s primitive scalar types to drive your system logic.
In Rust, a scalar type represents a single value. These are the building blocks from which all more complex data structures are constructed. Because they have a fixed size known at compile time, they are stored directly on the stack, making them extremely fast to work with.
There are four primary categories of scalar types:
1. Integers
Integers represent whole numbers. Rust provides signed (i) and unsigned (u) variants ranging from 8 bits to 128 bits, plus the architecture-dependent isize and usize.
let count: u32 = 10; // Unsigned integer (positive only)
let temperature: i8 = -5; // Signed integer (can be negative)
2. Floating-Point Numbers
These represent numbers with decimal points. Rust offers f32 (single precision) and f64 (double precision). By default, if you don't specify the type, Rust infers f64.
let pi = 3.14; // Defaults to f64
3. Booleans
The bool type is the backbone of control flow. It has only two possible values: true or false.
let is_running: bool = true;
4. Characters
In Rust, the char type represents a single Unicode Scalar Value. Unlike languages that treat characters as bytes, a Rust char is 4 bytes wide, allowing it to represent any Unicode character (including emojis).
let heart = '❤';
Why does this matter?
Because these types implement the Copy trait, they don't follow the complex move semantics we’ve discussed previously. When you assign one of these values to a new variable, Rust performs a "shallow copy," duplicating the bits on the stack. This makes them perfect for simple logic and counters where you don't want to worry about ownership or borrowing overhead. When building systems, choosing the right size (like u8 for a pixel color or f64 for high-precision math) ensures your data is memory-aligned and efficient.
Key Takeaway: Primitive scalar types are the stack-allocated, fixed-size foundations of your program that represent single values—integers, floats, booleans, and characters—which are safely and automatically copied by value.