Understanding Data Types in Rust
In this guide, we’ll explore the different data types in Rust, a powerful systems programming language. Knowing data types is essential for writing effective Rust programs.
Data Types in Rust
Rust has several built-in data types, which can be categorized into two main types: scalar and compound types.
- Scalar Types: These represent a single value.
i32: A 32-bit signed integer. Example:let age: i32 = 30;f64: A 64-bit floating point number. Example:let height: f64 = 5.9;char: A single character. Example:let letter: char = 'A';bool: A boolean value, eithertrueorfalse. Example:let is_student: bool = true;
- Compound Types: These can combine multiple values.
Tuple: A fixed-size collection of values of different types. Example:let person: (&str, i32) = ("Alice", 30);Array: A collection of elements of the same type with a fixed length. Example:let numbers: [i32; 5] = [1, 2, 3, 4, 5];
×