Everyday Types in Rust: Number Edition

Number Type

Everyday Types are a curated list of basic primitive types. They are unavoidable be it as a beginner or a veteran of Rust. Examples of these types are number, string, array, tuple, and function.

You will be exposed to the nitty-gritty of the number type in this article. Subsequent writings will discuss string, array, tuple etc.

💀

This article is targeted at audiences who have some basic exposure to Rust. If this is the first time you are trying Rust, visit my Hello Rust article to get a taste of what Rust can feel like.

Number Type

The Number type gives us a hint about what we expect a number variable to be.

At first, you might think this article is overkill but it isn't. Numbers in computers are not as easy as human sees them. For one good reason that human thinks in base 10 but the computer thinks in base 2 (Ones and Zeros).

While a lot of languages - Javascript for example - abstract how numbers work, Rust embraces it. Rust has little many subsidiaries of the number type. It can feel like a lot. However, after this section, you would fully understand and appreciate numbers and their type in Rust.

Numbers in Rust can be divided into two grand-category, Floats and Integers.

Floating Number Type

Floating Numbers, also referred to as floats, are decimal numbers in short - numbers with a dot in front of them. For example, 4.5, -3.455, 2.0, and 34334112.444 are valid Floating numbers while 2, 455, and 344112 are not. (no decimal in front of them 🤷‍♀️)

Please! take note from the previous paragraph, floating numbers are either positive or negative, -3.455, 4.55, 2.0. What matters is that they have decimals ⚡.

Floating numbers can further be broken down into:

  • signed floats

  • unsigned floats

From a layman's point of view, signed floats are negative floats, and unsigned floats are positives. While this definition can be somewhat true, it isn't completely correct.

A proper definition is signed floats as a data type represent decimal numbers that are "positive, negative, or zero". They are different from unsigned floats, which are limited to "positive or zero".

Examples of signed floats are -2.34, -4.23, 2.3 while unsigned floats are the positives 2.34, 4.23, 2.3.

Integer Number Type

Integer numbers also referred to as integers, are whole numbers including 0. For example 45, 345, 0, and -66 are integers, while 45.2, 345.0, and 0.454 are not.

Please! take note again, there are positive integers and negative integers, -3, 4, 2 ⚡. Integers can be further divided into signed integers (positive, negative and zero) and unsigned integers (positive integers only).

Variations of Number Types

Now that you are familiar with the bigger picture of how numbers are treated in Rust, let's highlight the actual number types.

  • Unsigned Integers: u8, u16, u32, u64, u128. Interpreted as an unsigned integer of 8 bits, 16 bits and so on.

  • Signed Integers: i8, i16, i32, i64, i128. Interpreted as a signed integer of 8 bits, 16 bits, 32 bits and so on.

  • Floating numbers types: f32, and f64. Interpreted as floats of 32 bits etc. It is noteworthy to mention that the floating type represent both signed and unsigned floats in Rust. They do not have a separate type for signed and unsigned floats like integers do.

    Did you notice no float type for 8 bits and 16 bits?

The 8, 16, 32 etc behind those letters are to indicate the limit of the number types. For example

  • 8 bits represent the number between 0 and (2^(n-1) - 1 where n = 8) = 127. When your figure passes the range of the number type, Rust will panic.

          let u_8: u8 = 123;
          let u_8i: u8 = 300; //Panic!! 300 > 127
    

    A signed integer of 8 bits, i8 must be in the range of -128 and 127.

    An unsigned integer of 8 bits, u8 must be in the range of 0 and 127.

    No float type of 8 bits.

  • 16 represent the number must be between 0 and (2^(n-1) - 1 where n = 16) = 32,767.

          let u_16: i16 = -12_003;
          let u_16i: i16 = 33_000; //Panic!! 33,000 > 32,767
    

    A signed integer of 16 bits, i8 must be in the range of -32768 and 32767.

    An unsigned integer of 16 bits, u8 must be in the range of 0 and 32767.

    No float type of 16 bits.

On a closing note, fill in the appropriate number type in this code below. Check out the correct answer after attempting it.

fn main (){
     let a: ?? = -2323;
    let b: ?? = 22.3;
    let c: ?? = 323;

    println!("{} {} {}", a, b, c);
}

A big thank you for reading thus far. I put a lot of intent into this article and I can only hope you did find it useful. Whether you are looking at learning more about Rust, subscribe to my article.

PeaceOut!!!!!!!


#Footer

extensive explanation of how numbers work in computers -