Welcome to the Scalar Types Overview lesson! In this tutorial, we'll explore the fundamental data types in Rust, also known as scalar types. These are the building blocks for more complex data structures in Rust. Let's dive right in!
Scalar types are the simplest types of data available in Rust, and they include:
i32i16i8u32u16u8f32f64charboolEach scalar type represents a specific kind of data, such as integers, floating-point numbers, characters, and boolean values.
Integers are whole numbers without decimal points. Rust provides several integer types, each with a different range of values:
i32: Signed 32-bit integers, range: -(2^31) to 2^31 - 1i16: Signed 16-bit integers, range: -(2^15) to 2^15 - 1i8: Signed 8-bit integers, range: -(2^7) to 2^7 - 1Note that smaller integer types use less memory, but can only represent a smaller range of values.
Unsigned integers are non-negative whole numbers. Rust also provides unsigned integer types for each of the signed integer types mentioned above:
u32: Unsigned 32-bit integers, range: 0 to 2^32 - 1u16: Unsigned 16-bit integers, range: 0 to 2^16 - 1u8: Unsigned 8-bit integers, range: 0 to 2^8 - 1Floating-point numbers are used to represent real numbers. Rust provides two types of floating-point numbers:
f32: Single-precision floating-point numbers, with 32 bits of precisionf64: Double-precision floating-point numbers, with 64 bits of precisionFloating-point numbers are useful when dealing with decimal numbers, and they are used extensively in scientific and financial computations.
The char type in Rust represents a single Unicode character. Characters are enclosed in single quotes, such as:
let my_char: char = 'A';The bool type in Rust represents a boolean value, which can be either true or false. Boolean values are often used in conditions and control structures.
Now that we've covered the different scalar types, let's see how to work with them. Here's a simple example of working with integers, floating-point numbers, characters, and boolean values:
fn main() {
// Integers
let my_i32: i32 = 123;
let my_i8: i8 = 7;
// Unsigned integers
let my_u32: u32 = 456;
let my_u8: u8 = 255;
// Floating-point numbers
let my_f32: f32 = 3.14;
let my_f64: f64 = 2.718281828;
// Character
let my_char: char = 'A';
// Boolean
let my_bool: bool = true;
// Printing scalar values
println!("My i32 value is: {}", my_i32);
println!("My i8 value is: {}", my_i8);
println!("My u32 value is: {}", my_u32);
println!("My u8 value is: {}", my_u8);
println!("My f32 value is: {}", my_f32);
println!("My f64 value is: {}", my_f64);
println!("My char value is: {}", my_char);
println!("My boolean value is: {}", my_bool);
}Question: Which Rust scalar type is used to represent a single Unicode character?
A: i32
B: u32
C: char
Correct: C
Explanation: The char type in Rust represents a single Unicode character.
That's it for this lesson on Rust scalar types! In the next lesson, we'll explore compound data types in Rust, such as arrays, tuples, and structs. Stay tuned! 🚀