Back
Close

Rust for Python Developers - Unsigned, Signed Integers and Casting

Shin_O
30.2K views

This story was originally published on Medium

Follow me:

Introduction

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:

// When we operate different data types, it fails:
fn main() {
    let a: i16 = 2;
    let b: u16 = 4;
    println!("{}", a+b);
}

And why casting 128 to i8 is -128.

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

We will also cover adding a negative number, bitwise negation, and converting a binary to an unsigned and signed decimal.

Let’s start from Rust integer types first.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io