5rustdatatypes

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, either true or false. 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];

Note: We aim to make learning easier by sharing top-quality tutorials, but please remember that tutorials may not be 100% accurate, as occasional mistakes can happen. Once you've mastered the language, we highly recommend consulting the official documentation to stay updated with the latest changes. If you spot any errors, please feel free to report them to help us improve.

top-home