Data Types in Rust Language
The Type System in Rust represents the various data types supported by the language. It ensures that values are
valid before they are used in a program, helping to prevent unexpected behavior. Additionally, Rust’s Type System provides enhanced code hinting and automated documentation.Rust is a statically typed language, meaning that every value must have a specific data type. The compiler can automatically infer the data type of a variable based on the value assigned to it.
To declare a variable in Rust, you use the let
keyword, like this:
fn main() {
let company_string = "TutorialsPoint"; // string type
let rating_float = 4.5; // float type
let is_growing_boolean = true; // boolean type
let icon_char = '♥'; // Unicode character type
println!("company name is: {}", company_string);
println!("company rating on 5 is: {}", rating_float);
println!("company is growing: {}", is_growing_boolean);
println!("company icon is: {}", icon_char);
}
In the code above, the data type of the variables is inferred from the values assigned to them. For example, Rust assigns a string data type to the company_string
variable and a float data type to rating_float
.
The println!
macro is used to display output, and it takes two arguments: a placeholder enclosed in {}
and the variable name or a constant. The placeholder is replaced with the variable’s value when displayed.
The output of the code will be:
company name is: TutorialsPoint
company rating on 5 is: 4.5
company is growing: true
company icon is: ♥
Rust has four primary scalar types: Integer, Floating-point, Booleans, and Characters. Each type serves a specific purpose:
Integer: Represents whole numbers and can be signed (allowing negative and positive values) or unsigned (positive values only). Rust provides various integer sizes, such as i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, and arch (architecture-dependent size).
Floating-point: Comes in two flavors, f32 (single-precision float) and f64 (double precision).
Booleans: Represents true or false values.
Characters: Represents Unicode scalar values, which can be letters, numbers, special characters, or emojis.
Rust ensures that values don’t exceed the valid range for their data type. For example, an integer overflow occurs when a value exceeds the defined range for its data type, resulting in unexpected behavior. Rust provides warnings or errors in such cases.
Here’s a quick summary of these types:
- Integers can be signed (i8, i16, i32, i64, i128, isize) or unsigned (u8, u16, u32, u64, u128, usize). The
arch
size depends on the machine’s architecture. - Floats include f32 and f64, with f64 being the default type.
- Rust doesn’t allow automatic type casting, ensuring type safety.
- You can use underscores as visual separators for large numbers (e.g., 50,000).
- Booleans have values true or false.
- Characters support Unicode scalar values, which can represent a wide range of characters, including emojis.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.