Rust is a statically typed programming language, which means that the type of every variable must be known at compile time. This helps catch errors early and makes Rust programs more reliable. In this section, we will explore the basic data types available in Rust, including scalar types (such as integers, floating-point numbers, booleans, and characters) and compound types (such as tuples and arrays).
Scalar types represent a single value. Rust has four primary scalar types: integers, floating-point numbers, booleans, and characters.
Integers are whole numbers without a fractional component. Rust provides several integer types with varying sizes:
i8, i16, i32, i64, i128: Signed integers (can be negative).u8, u16, u32, u64, u128: Unsigned integers (cannot be negative).The default integer type is i32 because it provides a good balance between size and performance on most systems.
Floating-point numbers are numbers with a fractional component. Rust has two floating-point types:
f32: Single-precision floating point.f64: Double-precision floating point (default).Booleans represent true or false values. The type is written as bool.
Characters in Rust are represented by the char type, which is four bytes in size and represents a Unicode Scalar Value.
Compound types can group multiple values into one type. Rust has two primitive compound types: tuples and arrays.
A tuple groups a number of values with a variety of types into one compound type. Tuples have a fixed length:
let tup = (500, 6.4, 'z');
You can access the elements of a tuple using pattern matching or dot notation:
let (x, y, z) = tup;
println!("The value of y is: {}", y);
Or:
let five_hundred = tup.0;
let six_point_four = tup.1;
let z = tup.2;
An array in Rust has a fixed size and can store elements of the same type:
let a = [1, 2, 3, 4, 5];
You can access elements of an array using indexing:
let first = a[0];
let second = a[1];
Here's an example demonstrating the use of different integer types:
1fn main() {2let x: i8 = 5;3let y: u32 = 42;4println!("x is {} and y is {}", x, y);5}
Here's an example of using floating-point numbers:
1fn main() {2let a = 2.0; // f643let b: f32 = 3.14; // f324println!("a is {} and b is {}", a, b);5}
Here's an example of using booleans:
1fn main() {2let t = true;3let f: bool = false; // with explicit type annotation4println!("t is {} and f is {}", t, f);5}
Here's an example of using characters:
1fn main() {2let c = 'z';3let z: char = 'ℤ'; // with explicit type annotation4println!("c is {} and z is {}", c, z);5}
Here's an example of using tuples:
1fn main() {2let tup = (500, 6.4, 'z');3let (x, y, z) = tup;4println!("The value of x is: {}", x);5println!("The value of y is: {}", y);6println!("The value of z is: {}", z);7}
Here's an example of using arrays:
1fn main() {2let a = [1, 2, 3, 4, 5];3let first = a[0];4let second = a[1];5println!("The first element is {} and the second element is {}", first, second);6}
In the next section, we will explore operators in Rust, which allow you to perform operations on data types. Understanding operators is crucial for performing calculations and manipulating data in your programs.
Stay tuned!