In Rust, data types are a crucial part of the language, ensuring that your programs are safe and efficient. Rust is a statically typed language, which means that the type of a variable must be known at compile time. This module will cover the various data types available in Rust, how to use them, and their specific characteristics.
Key Concepts
-
Scalar Types
- Integers
- Floating-point numbers
- Booleans
- Characters
-
Compound Types
- Tuples
- Arrays
-
Type Inference
-
Type Annotations
Scalar Types
Integers
Rust provides several integer types, each with a specific size. The size of an integer determines the range of values it can hold.
Type | Size (bits) | Range |
---|---|---|
i8 |
8 | -128 to 127 |
i16 |
16 | -32,768 to 32,767 |
i32 |
32 | -2,147,483,648 to 2,147,483,647 |
i64 |
64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
u8 |
8 | 0 to 255 |
u16 |
16 | 0 to 65,535 |
u32 |
32 | 0 to 4,294,967,295 |
u64 |
64 | 0 to 18,446,744,073,709,551,615 |
Example:
Floating-point Numbers
Rust supports two floating-point types: f32
and f64
.
Type | Size (bits) | Precision |
---|---|---|
f32 |
32 | Single |
f64 |
64 | Double |
Example:
Booleans
Booleans are simple types that can be either true
or false
.
Example:
Characters
Characters in Rust are represented by the char
type and are Unicode scalar values.
Example:
fn main() { let letter: char = 'A'; let emoji: char = '😊'; println!("letter: {}, emoji: {}", letter, emoji); }
Compound Types
Tuples
Tuples group multiple values of different types into a single compound type.
Example:
fn main() { let tuple: (i32, f64, char) = (42, 3.14, 'A'); let (x, y, z) = tuple; // Destructuring println!("x: {}, y: {}, z: {}", x, y, z); }
Arrays
Arrays in Rust are collections of elements of the same type and have a fixed length.
Example:
Type Inference
Rust can often infer the type of a variable based on the value assigned to it.
Example:
fn main() { let x = 42; // Rust infers `x` to be of type `i32` let y = 3.14; // Rust infers `y` to be of type `f64` println!("x: {}, y: {}", x, y); }
Type Annotations
Sometimes, you may need to explicitly specify the type of a variable.
Example:
Practical Exercises
Exercise 1: Define and Print Variables
Define variables of different types and print them.
fn main() { let integer: i32 = 10; let float: f64 = 20.5; let boolean: bool = true; let character: char = 'R'; println!("Integer: {}, Float: {}, Boolean: {}, Character: {}", integer, float, boolean, character); }
Exercise 2: Use Tuples and Arrays
Create a tuple and an array, then print their elements.
fn main() { let tuple: (i32, f64, char) = (10, 20.5, 'R'); let array: [i32; 3] = [1, 2, 3]; println!("Tuple: ({}, {}, {})", tuple.0, tuple.1, tuple.2); println!("Array: [{}, {}, {}]", array[0], array[1], array[2]); }
Common Mistakes and Tips
- Integer Overflow: Be cautious of integer overflow. Rust will panic in debug mode if an integer overflows.
- Type Mismatch: Ensure that the types of variables match the expected types in operations.
- Array Indexing: Accessing an array out of bounds will cause a panic.
Conclusion
In this section, we covered the basic data types in Rust, including scalar and compound types. Understanding these types is fundamental to writing safe and efficient Rust programs. In the next module, we will delve into functions, which will allow us to organize and reuse our code effectively.