Rust - Data Types: Scalar

1 minute read

:information_source: Note: Read the offical docs to get the most updated information.

Data Types

Rust doesn’t do any implicit type conversion. In cases when many types are possible we must add a type annotation, like this: : u32

let guess: u32 = "42".parse().expect("Not a number!");
  • When converting from a larger type to a smaller one (for instance u64 to u32) you could lose data.
  • Converting from a floating point to an integer will lose everything behind the decimal point, effectively rounding down.

Rust has two data type subsets: scalar and compound.

Scalar Types: Basic Types

A scalar type represents a single value. Rust has four primary scalar types: (integers, floating-point) Numbers, Booleans, and characters.


let t = true;
let f: bool = false; // with explicit type annotation


let c = 'z';
let z: char = 'ℤ'; // with explicit type annotation
let heart_eyed_cat = '😻';
  • specify char literals with single quotes, as opposed to string literals, which use double quotes.
  • char type is four bytes in size and represents a Unicode Scalar Value.

Integer Numbers

Numbers without a fractional component.

Length:  Signed	Unsigned
8-bit:   i8     u8
16-bit:  i16    u16
32-bit:  i32    u32
64-bit:  i64    u64
128-bit: i128   u128
arch:    isize  usize # 64 bits if you’re on a 64-bit architecture
                  # and 32 bits if you’re on a 32-bit architecture

You can write integer literals in

Decimal: 98_222 # _ is visual separator of 98222
Hex: 0xff
Octal: 0o77
Binary: 0b1111_0000
Byte: b'A' # u8 only

Number literals that can be multiple numeric types allow a type suffix, such as 57u8, to designate the type. integer types default to i32 (e.g when indexing some sort of collection).

:warning: Note: If you try to change the variable to a value outside that range, integer overflow will occur:

  • When you’re compiling in debug mode, Rust includes checks for integer overflow that cause your program to panic at runtime if this behavior occurs.
  • When you’re compiling in release mode with the --release flag, Rust does not check it. Instead, Rust performs two’s complement wrapping: values greater than the maximum value the type can hold “wrap around” to the minimum of the values the type can hold.

Floating-Point Numbers

Numbers with decimal points (IEEE-754 Standard) e.g. 97.51 , has two primitive types: f32 and f64 (default).


Leave a Comment