Numeric Types

To a first glance built-in integer types are very similar to the types available in C/C++. Table 2-1: Integer Types in Rust

LengthSignedUnsigned
8-biti8u8
16-biti16u16
32-biti32u32
64-biti64u64
128-biti128u128
arch-bitisizeusize

u32 basics

Actually rust build-in types are basically classes std u32.

Rust does most of the checks in compiling time, try to execute the following code blocks.

When possible overflow are detected at compile time.

fn main() {
    let a = 200u8;  // type specified
    let b = 200;    // type inference
    let _c = a + b;
}

The default reaction of rust is to panic in case of runtime error.

#![feature(strict_overflow_ops)]
fn main() {
    let a = 200u8;
    let b = 200u8;
    let _c = a.strict_add(b);
}

Types are more similar to classes with convenient basic functionalities.

fn main() {
    let a = 200u8;
    let b = 200u8;
    let c = a.saturating_add(b);
    println!("{c}");
}
fn main() {
    let a = 200u8;                  // by default all variables are immutable
    let mut c = a.checked_add(1);   // mut: to declare a mutable variable
    println!("{:?}", c);

    let mut c = (u32::MAX - 2).checked_add(3);
    println!("{:?}", c);
}