Basic Rust Data types

ยท

3 min read

Data is classified into different types based on their category, such as integers for signed numbers (+, -), floats for numbers with decimal points (3.4), char for characters ('a'), and Strings for sequences of characters ("Hello, how are you"). However there are 4 basic data types which will see .

Basic data types

Integers

Integers are a type of number in programming that can have either positive or negative values. They are commonly represented by types such as i32 or u32 in many programming languages. The u32 type stands for an unsigned 32-bit integer, which means it can only hold non-negative values, ranging from 0 to 4,294,967,295. On the other hand, the i32 type represents a signed 32-bit integer, capable of holding both negative and positive values, ranging from -2,147,483,648 to 2,147,483,647. The number following the letter, such as 32 in i32, indicates the amount of space it occupies, measured in bits. Understanding these types helps developers make informed decisions about which type to use based on the requirements of their specific cases . Here ' s a table about our content .

LENGTHSIGNEDUNSIGNED
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
archisizeusize

FLOAT

Floats, or floating-point numbers, are essentially numbers that include decimal points. Unlike integers, which are whole numbers, floats can represent fractions and more precise values. They are particularly useful in situations where more precision is required, such as scientific calculations or when dealing with currency. Floats are represented by types such as f32 and f64. The 'f' stands for float, and the number indicates the amount of space they occupy in computer memory. For instance, f32 uses 32 bits of memory, while f64 uses 64 bits. This difference in memory space allows f64 to store numbers with greater precision compared to f32.

LENGTHSIGN
32 - bitf 32
64 - bitf 64

CHAR & STRING

Char is short for Character. It is defined by single quotes (' '). Characters can include any value, such as emojis, symbols, numbers, letters, and other types of characters. For example, a Char could be 'a', '1', or even an emoji like '๐Ÿ˜Š'. Here is an example of a Char: 'Wow'. Each Char occupies 4 bytes in memory, which allows it to store any Unicode means any type of symbols and scalar value.

Strings, on the other hand, are sequences of characters. They are defined by double quotes (" "). Inside the double quotes, you can include any text you want just like in Chars. Strings are more complex than Chars because they can be of any length . For instance, a String can be "Hello, how are you?" or "12345". In Rust, you can use the println! print_line macro to print Strings to the console. Here is an example:

let greeting = "Hello, world!";
println!("{}", greeting);

In this example, the println! macro takes the String greeting and prints it to the console. Strings in Rust are dynamic, meaning they can grow and shrink in size. This makes them very flexible for handling text data. Additionally, Rust provides various methods to manipulate Strings, such as concatenation, slicing, and iteration over characters which we will learn later .

Note :

Please kindly check the previous Rust Basics article series for updates now an then.

ย