I wanted to talk about basic types in Rust. Lets begin with Numerical Types in Rust, since Rust is a statically typed language, which means that it must know the types of all variables at compile time. I will talk about number types that are built into the language.
Numerical Types
Numerical values are divided into Integer Types and Floating-point type. A simple numerical type can be signed integer types or unsigned integer types.
Integer Data types are listed below:
- i8 : The 8-bit signed integer type.
- i16 : The 16-bit signed integer type.
- i32 : The 32-bit signed integer type.
- i64 : The 64-bit signed integer type.
- u8 : The 8-bit unsigned integer type.
- u16 : The 16-bit unsigned integer type.
- u32 : The 32-bit unsigned integer type.
- u64 : The 64-bit unsigned integer type.
- isize : The pointer-sized signed integer type.
- usize : The pointer-sized unsigned integer type.
Type | MAX | MIN |
i8 | 127 | -128 |
i16 | 32767 | -32768 |
i32 | 2147483647 | -2147483648 |
i64 | 9223372036854775807 | -9223372036854775808 |
i128 | 170141183460469231731687303715884105727 | -170141183460469231731687303715884105728 |
u8 | 0 | 255 |
u16 | 0 | 65535 |
u32 | 0 | 4294967295 |
u64 | 0 | 18446744073709551615 |
u128 | 0 | 340282366920938463463374607431768211455 |
let a = 100; // The default integer type in Rust is i32
let b: i8 = -128; //explicitly telling it will be an i8 type
let x = 2i8; // Equals to `let x: i8 = 2;` personally I don't like this way of, I prefer the previous statement rather
Floating Point data types are simplier, there is only 2 types.
- f32 : The 32-bit floating point type.
- 64 : The 64-bit floating point type.
let x = 1.5; // The default float type in Rust is f64
let y: f32 = 2.0; // setting default to f32
Summary
This was a short intro to the numerical values and the limits of each value in Rust.