Scalar Types
Scalar types represent a single value. Rust offers four primary scalar types: integers, floating-point numbers, booleans, and characters.Integers
Integers are numbers without fractional components. Rust provides several integer types, which can be either signed or unsigned. Signed integers can represent both positive and negative numbers, while unsigned integers represent only positive values.
i32 type (a 32-bit signed integer), but you can specify other types as needed.

Floating-Point Numbers
Floating-point numbers include a fractional component. Rust has two floating-point types:f32 and f64. The default is f64 due to its higher precision.
Booleans
The boolean type,bool, can hold one of two values: true or false. These values are typically used in conditional expressions and logic.
Characters
Thechar type represents a Unicode scalar value and occupies four bytes of memory. It supports a vast range of characters beyond basic ASCII.
Compound Types
Compound types group multiple values into one type. Rust provides two primitive compound types: tuples and arrays.Tuples
Tuples allow you to combine values of different types into a single compound type. Once declared, the length of a tuple cannot change. Consider the following example, where a tuple is declared with ani32, an f64, and a u8:
Arrays
Arrays in Rust are collections of values of the same type with a fixed length. Array indexing starts at 0 and goes up to the length of the array minus one. Below is an example of creating an array and accessing its elements:Out-of-Bounds Access
Accessing an array element at an index outside its bounds will compile successfully but result in a runtime panic. For example, attempting to access index 6 in an array of five elements causes a panic.Accessing an invalid array index will cause your program to panic at runtime. Rust performs runtime checks to ensure memory safety. Always ensure your indices are within range.
Handling Invalid Access Gracefully
To safely access array elements, use theget method, which returns an Option. This allows you to handle out-of-bound indices without causing a runtime panic.
Summary
In this lesson, we’ve covered the basics of Rust’s data types:-
Scalar Types:
- Integers: Numbers without fractional parts, with support for various formats and both signed and unsigned integers.
- Floating-Point Numbers: Numbers with decimals, available as
f32andf64, withf64as the default. - Booleans: The
booltype, representingtrueorfalse. - Characters: The
chartype for Unicode scalar values.
-
Compound Types:
- Tuples: Fixed-length collections of values of different types.
- Arrays: Fixed-length collections of elements of the same type, including techniques for handling out-of-bound access.