1
Rust has signed integers (denoted by **i**) and unsigned integers (denoted by **u**) that consist of 8, 16, 32, 64, and 128 bits. The math behind binary notation is not relevant for the scope of this book. What we do need to understand, though, is the range of numbers allowed in terms of bits. Because binary is either 0 or 1, we can calculate the integer range by raising two to the power of the number of bits. For example, for 8 bits, 2 to the power of 8 equates to 256. Considering the 0, this means that an **i8** integer should have a range of 0 to 255, which can be tested by using the following code:
let number: u8 = 255;
Let's take a look at the following code:
```rust
let number: u8 = 256;
```
It's not surprising that the preceding code gives us the following overflow error:
```rust
literal `256` does not fit into the type `u8` whose range is ``0..=255
```
What's mot expected is if we change it to a singned integer:
```rust
let number: i8 = 255;
```
Here, we get the following error:
```rust
literal `255` does not fit into the type `i8` whose range is `-128..=127`
```
This is because unsigned integers only house positive integers and signed integers house positive and negative integers. Since bits are memory size, the signed integer has to accommodate a range on both sides of zero, so the modulus of the signed integers is essentially half.
In terms of floats, Rust accommodates **f32** and **f64** floating points, which can be both negative and positive. Declaring a floating-point variable requires the same syntax as integers:
let float: f32 = 20.6;
It has to be noted that we can also annotate numbers with suffixes, as shown in the following code:
let x = 1u8;
Here, x has a value of 1 with the type of u8. Now that we have covered floats and integers, we can use vectors and arrays to store them.
You must log in or # to comment.