scalar data types in Rust represent a single value. They are among the basic types or primitives in the language, serving as building blocks for more complex types.
Rust has four primary scalar types:
- integers
- floating-point numbers
- Booleans
- characters
Integers
- Integers are numbers without a fractional component.
- Rust provides various integer types with specific sizes, denoted by a prefix (i for signed, u for unsigned) and the number of bits (8, 16, 32, 64, 128). Examples include i8, u8, i32, u32, i64, u64, i128, u128.
- There are also isize and usize types, which are signed and unsigned integers whose size depends on the architecture of the machine (32 or 64 bits, the same size as an address).
- Integer literals can be written in decimal, hexadecimal (0x), octal (0o), or binary (0b).
- Underscores (_) can be used within numeric literals for readability, such as 1_000.
- You can optionally add a type suffix to a numeric literal to specify its type, for example, 123_i64 or 10_u16.
- The range of values an integer type can hold depends on its size and whether it is signed or unsigned.
- Rust supports standard arithmetic operations like addition, subtraction, multiplication, division, and remainder (%) on integers.
Example:
fn main() {
let decimal = 98_222; // Inferred integer type (often i32)
let hex = 0xff; // Inferred integer type
let octal = 0o77; // Inferred integer type
let binary = 0b1111_1111; // Inferred integer type (value 255)
let explicit_u32: u32 = 42; // Explicit type annotation
let signed_8bit: i8 = -5; // Explicit i8
let unsigned_16bit: u16 = 0x400; // Explicit u16 (value 1024)
let machine_size_usize: usize = 137; // Explicit usize
let sum = 5 + 10; // Basic addition
let remainder = 43 % 5; // Remainder operation
print!("{},{},{},{},{},{},{},{},{},{}",decimal,hex,octal,binary,explicit_u32,signed_8bit,unsigned_16bit,machine_size_usize,sum,remainder);
}
Output:
98222,255,63,255,42,-5,1024,137,15,3
Floating-Point Numbers
- Represent real numbers.
- Rust has two primitive floating-point types: f32 for single-precision (32 bits) and f64 for double-precision (64 bits). These types adhere to the IEEE 754 specification.
- They can be positive or negative and support special values like Infinity and NaN (Not a Number).
- For type inference, the default floating-point type is f64.
- Arithmetic operations like addition, subtraction, multiplication, and division are supported.
Examples:
fn main() {
let x = 1.61803; // Inferred floating-point type (default f64)
let x_f32: f32 = 3.14; // Explicit f32
let x_f64: f64 = 6.0221e23; // Explicit f64 with exponential notation
let xx_f32: f32 = 20.6;
let xx = 95.5 - 4.3; // Subtraction
let xxx= 56.7 / 32.2; // Division
let xxxx = f64::NAN; // Accessing the NaN constant
print!("{},{},{},{},{},{},{}",x,x_f32,x_f64, xx_f32,xx,xxx,xxxx)
}
Output:
1.61803,3.14,602210000000000000000000,20.6,91.2,1.7608695652173911,NaN
Booleans
- The boolean type, specified as bool, has only two possible values:
- Yes / no
- On / off
- True / false
- Booleans are one byte in size.
- They are commonly used in conditional expressions, such as if statements.
Examples:
fn main() {
let age = 30;
let can_vote = age >= 58;
println!("Can vote? {}", can_vote);
}
Output:
Can vote? false
Example:
fn main() {
let x = 8;
if x>5 {
println!("It's Correct");
} else {
println!("It's Wrong.");
}
}
Output:
It's Correct
Characters
- Rust’s char type represents a single Unicode Scalar Value.
- It is the language’s most primitive alphabetic type.
- A char is four bytes in size.
- char literals are specified using single quotes (‘), in contrast to string literals which use double quotes (“).
- char literals can include various escape sequences, such as \n for newline, \\ for a backslash, \xNN for a byte value, or \u{NNNN} for a Unicode code point.
- The standard library provides methods for characters, such as is_alphabetic().
Examples:
fn main() {
let c = 'z'; // Represents the character 'z'
let unicode_char = '字'; // Represents a Chinese character
let escaped_char = '\t'; // Represents a tab character
let special_char = '\u{CA0}'; // Represents a Unicode character by code point
// let byte_literal = b'A'; // NOTE: b'A' is a u8 byte literal, not a char type.
print!("{},{},{},{}",c,unicode_char,escaped_char,special_char)
}
Output:
z,字, ,ಠ