Rust has several primitive data types, which are the fundamental building blocks for more complex data structures. Here are the most commonly used primitive data types in Rust:
Integer types:
Signed integers: i8, i16, i32, i64, i128
Unsigned integers: u8, u16, u32, u64, u128
The numbers in these type names represent the number of bits used to store the value. Signed integers can store both positive and negative values, while unsigned integers can only store non-negative values.
Floating-point types:
f32: 32-bit single-precision floating-point number
f64: 64-bit double-precision floating-point number
Boolean type: bool: A type representing true or false values.
Character type: char: A 32-bit Unicode scalar value representing a single Unicode character.
Tuple type:
Tuples are fixed-size collections of values, where each value can have a different type. They are denoted using parentheses () and comma-separated values. For example, (i32, f64, bool) is a tuple type containing an i32, an f64, and a bool.
Array type:
Arrays are fixed-size collections of values with the same type. They are denoted using square brackets [] and a semicolon ; to separate the length from the element type. For example, [i32; 5] is an array type containing 5 elements of type i32.
Slice type:
Slices are similar to arrays but with a dynamically-sized length. They are denoted using the & symbol and square brackets []. For example, &[i32] is a slice type containing elements of type i32.
Reference type:
References are pointers to a value of a particular data type. They are denoted using the & symbol followed by the data type. For example, &i32 is a reference to a value of type i32.
These primitive data types are the foundation for building more complex data structures in Rust, and they can be combined in various ways to express more complicated data and behavior.