Rust Programming: Data Types (Section-1)

Assalamu'alaikum wr. wb

Hasil gambar untuk data type rustEvery value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data. We'll look at two data type subsets: scalar and compound. Keep in mind that Rust is statically typed language,which means that it must know the types of all variables at compile time. The compiler can usually infer what type we want to use based on the value and how we use it. In cases when many types are possible, such as when we conferted a String to a numeric type using parse, we must add a type annotation like this :

let guess: u32 = "42".parse().expect("Not a number...");

If we don't add a annotation here, Rust will tell us with display the error which means the compiler needs more information from us to know which type we want to use.


Scalar Types

A scalar type represent a single value. Rust has four primary scalar types: Integer, Floating-Point Number, Booleans and Characters. We can recognize these from other programming languages. Let's jump into how they work in Rust.

  • Integer
An integer is a number without a fractional component. This type declaration indicates that the value it's associated with should be an unsigned integer (signed integer types start with i, instead of u) that taakes up 32 bits of space. Each variant in the signed and unsigned columns can be used to declare the type of an integer value.
Information below shows us the built-in integer type in Rust :

Lenght Signed Unsnigned
8 bit i8 u8
16 bit i16 u16
32 bit i32 u32
64 bit i64 u64
128 bit i128 u128
arch isize usize

Each variant can be either signed  or unsigned and has explicit size. Signed and unsigned refer to whether it's possible for the number to be negative of positive. In other words, whether the number needs to have a sign with it (signed) or whether it will only ever be positive and can therefore be represented without a sign (unsigned). It's like writing numbers on paper, when the sign matters, a number is shown with a plus sign or a minus sign; However when it's safe to assume the number is positive, it's shown with no sign. Signed numbers are stored using two's complement representation.

Each signed variant can store numbers from -(2n-1) to 2n-1-1 inclusive, where n is the number of bits that variant uses. So an i8  can store numbers from -(27) to 27-1, which equals -128 to 127. Unsigned variants can store numbers from 0 to 2n-1, so a u8 can store numbers from 0 to 28-1, which equal 0 to 255.

Additionally, the isize and usize types depend on the kind of computer your program running on 64 bits if our computer architectur is 64 bits and 32 bits if our computer architectur is 32 bits.

We can write integer literals in any of the forms below. Note! That all number literals except the byte literal allow a type suffix, such as 57u8 and _ as a visual separator such as 1_000.

Numbers Literals Example
Octal 0o77
Binary 0b1111_0000
Byte (u8 Only) b'A'

So how do if we don't know which integer to use ? If not ensure, Rust default are generally good choices, and integer types default to i32. This type is generally the fastest even on 64 bits architectur system. The primary situation in which you'd use isize or usize is when indexing some sort of collection.

Now, we'll begin discuss about integer overflow...
Let's say we'rehave u8, which can hold values between 0 - 255. What happen if we try to change it to 256 ? This is called "Integer Overflow" and Rust has some interesting rules around this behavior. When compiling in debug mode, Rust checks for this kind of issue and will cause our program to panic, which is term Rust uses when a program exist with an error. 

In release builds, Rust doesn't check for overflow and instead will do something called "two's complement wrapping". In short, 256 become 0, 257 become 1, etc. Relying on overflow is considered an error, even if this behavior happens. If want this behavior explicity, the standard library has a type, Wrapping, that's provides it explicity.
  • Floating-Point
Rust also has two primitive types for floating-point numbers, which are numbers with decimal points. Rust's floating point types are f32 and f64, which are 32 bits and 64 bits in size, respectively. The default types is f64 because on modern CPU's it's roughly the same speed as f32 but is capable of more precision.

Here's an example that shows floating-point  numbers in action :

fn main(){
lex x = 2.0; //f64
let y: f32 = 3.0; //f32

Floating-point numbers are represented according the IEEE-754 standard. The f32 type is a single-precision float and f64 has double precision.
  • Number
Rust's support every mathamatical operation you'd expect for all number types; The following code shows us how work with it :

fn main(){
let sum = 5 + 7;
let difference = 95.5 - 8;
let product = 5 * 5;
let quotion = 78 / 4;
let remainder = 10 % 7;
}

Each expression in these statements uses a mathematical operator and evaluates to a single value, which is then bond to a variable. Appendix B contains a list of all operators that Rust provides.
  • Boolean
As in most other programming languages, Boolean type in Rust has two possible values : true and false. The Boolean type in Rust is specified using bool. For example :

fn main() {
let t = true;
let f:bool = false
}

The main way to use Boolean values is through conditionals, such as and  if expression. Boolean are one byte in size.
  • Character
So far we've worked only with numbers, but Rust supports letters too. Rust's char type is the language's most primitive alphabetic type, and the following code shows one way to use it. 

Note:
That the char literal is specified with single quotes as opposed to string literals which use double quotes.

Rust's char type represent a Unicode scalar value, which means it can represent a lot more that just ASCII. Accented letters; Chinese, Japanese, Arabic and Korean characters; are all valid char values in Rust. Unicode Scalar Values range from u+0000to u+D7FF and u+0000 to u+10FFFF inclusive. However, a character isn't really a concept in Unicode, so our human intuition for what a character is may not macth up with what a char is in Rust.

So, maybe enaugh for this time guys... See you in next chapter ....

Wassalamu'alaikum wr. wb
Ahmad Istakim

Alumni dari jurusan Manajemen Informatika di Universitas Sains Al-Qur'an (UNSIQ ) Wonosobo. Tertarik dalam bidang pendidikan, teknologi komputasi dan disiplin ilmu keislaman ( Tafsir, Hadits, Arudl, Nahwu-Sharaf, Fiqh maupun Aqidah ) - https://s.id/blog-islamQ. Pernah juga mengenyam pendidikan di beberapa pesantren yang ada di Kab. Wonosobo dan Kab. Purworejo

Posting Komentar

Lebih baru Lebih lama