Rust has four main categories of scalar primitive types: integers, floating-point numbers, booleans, and characters. These types are built into the language, fixed in size, and form the foundation of all value handling in Rust programs.
To write useful programs in Rust, you need to work with basic values like numbers, text, and true-or-false conditions. These are called primitive data types. They are simple, fast, and used everywhere in Rust.
In this post, I will explain the four main primitive scalar types, how they are defined, and when to use each one. These types are different from JavaScript or Python, which use fewer types but allow more flexible behavior. Rust is strict and explicit, which helps catch bugs before your program runs.
If you are new to Rust, you may want to check out Variable Shadowing and Scope and Variables and Mutability first.
Integers
An integer is a whole number without decimals. Rust gives you many options based on size and whether the number is signed or unsigned.
Signed integers can store both negative and positive values, while unsigned integers can only store positive values.
Type | Signed | Size | Range |
---|---|---|---|
i8 | Yes | 8-bit | −128 to 127 |
u8 | No | 8-bit | 0 to 255 |
i16 | Yes | 16-bit | −32,768 to 32,767 |
u16 | No | 16-bit | 0 to 65,535 |
i32 | Yes | 32-bit | −2,147,483,648 to 2,147,483,647 |
u32 | No | 32-bit | 0 to 4,294,967,295 |
i64 | Yes | 64-bit | large range |
u64 | No | 64-bit | large range |
isize | Yes | pointer size | based on platform |
usize | No | pointer size | based on platform |
By default, Rust uses i32 when you write an integer like this:
let x = 42; // inferred as i32
You can choose another type explicitly:
let y: u8 = 200;
let z: i64 = -50000;
Floating-Point Numbers
For decimal values, Rust uses f32 (32-bit) and f64 (64-bit). The default is f64 because it is more precise.
let height = 1.75; // f64
let weight: f32 = 65.5;
You can perform math like:
let result = 3.5 * 2.0;
Booleans
A boolean has two possible values: true or false.
let is_active = true;
let is_ready: bool = false;
Use booleans with if conditions:
if is_active {
println!("It is active");
}
Rust does not allow non-boolean types in conditions. This is different from JavaScript, where if (5) is allowed. In Rust, you must write:
if number != 0 { }
Characters
Rust has a char type that represents a single Unicode character. It uses single quotes.
let letter = 'A';
let emoji = '😊';
A char is not a string. It stores a 4-byte Unicode scalar value, not just ASCII.
Summary of Default Types
Kind | Type | Notes |
---|---|---|
Integer | i32 | Default whole number |
Float | f64 | Default decimal |
Boolean | bool | true or false |
Character | char | Unicode, 4 bytes |
Type Safety and Overflow
Rust prevents errors by requiring the correct type and checking for overflow. For example:
let a: u8 = 255;
let b = a + 1; // will panic in debug mode
In release mode, Rust wraps the value (256 becomes 0), but in debug mode it will stop the program. This keeps your code safe.
Summary
Rust’s primitive data types include integers, floats, booleans, and characters. Each type has a clear role and fixed size. Unlike JavaScript, Rust does not guess types loosely. It uses strict typing to make your programs safer and more predictable.