Rust - Scalar Types
In Rust, scalar types represent single values. They are the most basic types in the language and are categorized into four groups: integers, floating-point numbers, Booleans, and characters. Here's a detailed explanation of each:
1. Integer Types
Integers are numbers without a fractional component. Rust provides both signed and unsigned integers with various sizes.
- Signed Integers: Represented by
i
followed by the number of bits (e.g.,i8
,i32
).- Range: From
-(2^(n-1))
to2^(n-1) - 1
.
- Range: From
- Unsigned Integers: Represented by
u
followed by the number of bits (e.g.,u8
,u32
).- Range: From
0
to2^n - 1
.
- Range: From
Type | Bit Width | Minimum Value | Maximum Value |
---|---|---|---|
i8 |
8 bits | -128 | 127 |
i16 |
16 bits | -32,768 | 32,767 |
i32 |
32 bits | -2,147,483,648 | 2,147,483,647 |
i64 |
64 bits | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
i128 |
128 bits | -170,141,183,460,469,231,731,687,303, 715,884,105,728 | 170,141,183,460,469,231,731,687,303,715, 884,105,727 |
isize |
Arch Dependent | Depends on the architecture (e.g., 32 bits on a 32-bit system, 64 bits on a 64-bit system) | |
u8 |
8 bits | 0 | 255 |
u16 |
16 bits | 0 | 65,535 |
u32 |
32 bits | 0 | 4,294,967,295 |
u64 |
64 bits | 0 | 18,446,744,073,709,551,615 |
u128 |
128 bits | 0 | 340,282,366,920,938,463,463,374,607,431, 768,211,455 |
usize |
Arch Dependent | Depends on the architecture (e.g., 32 bits on a 32-bit system, 64 bits on a 64-bit system) |
Example: Declaring and Using Integers
rustfn main() {
let x: i32 = 42; // Signed 32-bit integer
let y: u8 = 255; // Unsigned 8-bit integer
println!("x = {}, y = {}", x, y);
}
2. Floating-Point Types
Floating-point numbers are numbers with a fractional component. Rust provides two types:
f32
: 32-bit floating-point number (single precision).f64
: 64-bit floating-point number (double precision, default).
Both follow the IEEE-754 standard.
Example: Declaring and Using Floating-Point Numbers
rustfn main() {
let x: f64 = 3.14159; // 64-bit floating-point
let y: f32 = 2.71828; // 32-bit floating-point
println!("x = {}, y = {}", x, y);
}
3. Boolean Type
The Boolean type in Rust is represented by bool
. It can have one of two values:
true
false
Example: Declaring and Using Booleans
rustfn main() {
let is_rust_fun: bool = true;
let is_rust_hard = false; // Type inference
println!("Is Rust fun? {}", is_rust_fun);
println!("Is Rust hard? {}", is_rust_hard);
}
4. Character Type
The character type in Rust is represented by char
. It represents a single Unicode scalar value,
meaning it can represent more than just ASCII characters.
- Size: 4 bytes (32 bits).
Example: Declaring and Using Characters
rustfn main() {
let letter = 'R';
let emoji = '?';
println!("Letter: {}, Emoji: {}", letter, emoji);
}
Summary
- Integer Types: Signed (
i8
,i32
, etc.) and unsigned (u8
,u32
, etc.) integers with various bit widths. - Floating-Point Types:
f32
andf64
for single and double precision. - Boolean Type:
bool
, withtrue
orfalse
values. - Character Type:
char
, representing a single Unicode scalar value, with 4 bytes in size.
These scalar types are the building blocks of Rust's type system, allowing you to represent a wide range of values efficiently and safely.