published: 5th of February 2022
Integers represent whole numbers. Rust has both unsigned integers and signed integers. unsigned integers cannot be negative, and have a higher positive range than signed integers, that can be negative, but have a lower postive range.
The following table lists the integer literal types in Rust.
Length | Unsigned | Signed |
---|---|---|
8-bits | u8 | i8 |
16-bits | u16 | i16 |
32-bits | u32 | i32 |
64-bits | u64 | i64 |
128-bits | u128 | i128 |
CPU Architecture | usize | isize |
The following table lists the formats that are supported for an integer literal definition.
Literal | Example |
---|---|
Decimal | 42 |
Hex | 0x2a |
Octal | 0o52 |
Binary | 0b101010 |
Byte (u8 only) | b'Z' |
// i32 by default
let the_answser = 42;
// Annotate the type of integer
let dinner_for_two: i8 = 69;
// Integer suffixing is also supported for type annotation
let its_friday = 420u16; // => 420
// Underscores can help to improve readbility
let you_da_winnah = 1_16_32_5_12_19_40_i64; // => 116325121940
Floating points represent a number with a fractional component expressed in IEEE-754 format. Rust has two floating point types: f32 and f64 .
The following table list the floating point literal types and their bits of precision.
Precision | Type |
---|---|
32-bits | f32 |
64-bits | f64 |
// f64 by default
let highly_sexual = 420.69;
// Annotate the type of float
let dinner_for_two: f32 = 69.69;
// Float suffixing is also supported for type annotation
let its_friday = 420.69f32; // => 420.69
// Underscores can help to improve readbility
let you_da_winnah = 420_69.1337_f64; // => 42069.1337
https://www.manning.com/books/rust-in-action
https://www.udemy.com/course/ultimate-rust-crash-course/
https://www.techopedia.com/definition/16441/scalar
https://stackoverflow.com/questions/5739888/what-is-the-difference-between-signed-and-unsigned-int