Rust Scalar Types Overview 🎯

beginner
10 min

Rust Scalar Types Overview 🎯

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!

Understanding Scalar Types 📝

Scalar types are the simplest types of data available in Rust, and they include:

  • i32
  • i16
  • i8
  • u32
  • u16
  • u8
  • f32
  • f64
  • char
  • bool

Each scalar type represents a specific kind of data, such as integers, floating-point numbers, characters, and boolean values.

Integers 💡

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 - 1
  • i16: Signed 16-bit integers, range: -(2^15) to 2^15 - 1
  • i8: Signed 8-bit integers, range: -(2^7) to 2^7 - 1

Note that smaller integer types use less memory, but can only represent a smaller range of values.

Unsigned Integers 💡

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 - 1
  • u16: Unsigned 16-bit integers, range: 0 to 2^16 - 1
  • u8: Unsigned 8-bit integers, range: 0 to 2^8 - 1

Floating-point Numbers 💡

Floating-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 precision
  • f64: Double-precision floating-point numbers, with 64 bits of precision

Floating-point numbers are useful when dealing with decimal numbers, and they are used extensively in scientific and financial computations.

Character 💡

The char type in Rust represents a single Unicode character. Characters are enclosed in single quotes, such as:

rust
let my_char: char = 'A';

Boolean 💡

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.

Working with Scalar Types 📝

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:

rust
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); }

Quiz 💡

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! 🚀