Common Programming Concepts - Part 2 (Data Types)
What are Data Types? As we discussed in the last article that we can store any data in variables. The variables that we were declaring till now did not have any data types mentioned and were being assigned data type according to the data they contain by the Rust compiler. This is necessary because every data type requires a specific amount of space which is assigned after the data type is known.…
Data Types are an essential aspect of programming as they determine the amount of memory allocated for variables. In Rust, variables are statically typed, meaning their data type must be specified during compilation. There are two main types of data types in Rust: Scalar and Compound. We will focus on the Scalar types in this article.
Scalar Types represent a single value, such as numbers or characters. There are four types of scalar data types in Rust: Integers, Floating-point numbers, Booleans, and Characters.
Character Type:
The Character Type represents a single Unicode value, like 'a', '=', or '🦀'. In Rust, we declare a character type using the keyword 'char'. It occupies 4 bytes of memory. For instance, to declare a variable 'ch' with the character 'a', we would write: let ch: char = 'a';.
Boolean Type:
The Boolean type has two possible values: true or false. It is 1 byte in size and is commonly used in conditional statements to determine if a statement is true or false. To declare a boolean variable, we use the keyword 'bool'. For example, let flag: bool = true; creates a variable flag with a Boolean data type and the value true.
Integer Type:
Integer type is used to represent whole numbers, both positive and negative. However, due to the infinite nature of numbers, Rust provides various integer types with different sizes and signedness. Signed integers can hold both positive and negative values, while unsigned integers can only hold positive values. Rust provides sizes ranging from 8-bit to 128-bit integers, with both signed and unsigned variants.
Unsigned integers are used when a variable is guaranteed to have only positive values, while signed integers are used when negative values are possible.
Here's a list of all the integer types and their sizes:
- Length
- Signed
- Unsigned
- 8-bit: i8 u8
- 16-bit: i16 u16
- 32-bit: i32 u32
- 64-bit: i64 u64
- 128-bit: i128 u128
- Architecture-dependent: isize usize
Integer Overflow:
Integer Overflow is an error that occurs when a value is assigned to an integer variable that exceeds its defined size. For instance, u8 can only store values between 0 and 255. If we try to assign a value greater than 255 to a u8 variable, Rust will throw an error. Rust handles this error in two ways: in debug mode, it will panic and display an error message; in release mode, it will silently wrap around the value.
Floating-point Type:
Floating-point numbers are numbers with decimal points. Rust provides two floating-point types: f32 (32-bit) and f64 (64-bit). To declare a floating-point variable, we use the keywords 'f32' or 'f64'. For example, let fl: f32 = 2.22; creates a variable named fl and assigns the value 2.22 to it.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.