Table of Contents

🦀 Introduction
🦀 Unsigned Integer Types
🦀 Signed Integer Types
🦀 Sign-and-Magnitude
🦀 Ones’ Complement
🦀 Two’s Complement
🦀 4-bit Signed Binary Number Comparison
🦀 Casting in Rust
🦀 Casting to an Unsigned Type
🦀 Casting to a Signed Type
🦀 Conclusion


Rust has two data type subsets, scalar, and compound. The scalar types are integers, floating numbers, Booleans, and characters. The compound types are arrays and tuples.

In this article we are going to see why the following code fails:

fn main() {
    let a: i16 = 2;
    let b: u16 = 4;
    println!("{}", a+b);

And why casting 128 to i8 is -128.

Casting 128 to i8


128 as a i8 is : -128

To understand better about the casting, we need to review about Signed, Ones’ Complement and Two’s Complement.

Let’s start from Rust integer types first.

#rust #developer

Unsigned, Signed Integers and Casting for Rust Beginners
3.65 GEEK