Rust Types

Rust is a static-typed language.


Scalars

Integers

The default integer type is i32. In other words, x in the below example is an i32:

fn main() {
    let x = 2;
}

Length

Signed

Unsigned

8-bit i8

i8

u8

16-bit

i16

u16

32-bit

i32

u32

64-bit

i64

u64

128-bit

i128

u128

arch

isize

usize

Integer literals can also be expressed in any of the following ways:

Floating-point numbers

There are two floating-point types: f32 and f64. The default is f64.

Booleans

The bool types has one of two values: true or false. Note the lowercased keywords, as compared to Python's True.

Characters

The char type is 4 bytes wide and stores a Unicode Scalar Value. These range from U+0000 to U+D7FF and U+E000 to U+10FFFF inclusive.


Compounds

Tuples

Tuples are a group of values. They are fixed in size and type.

fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);

    //tuples can be destructured
    let (x, y, z) = tup;

    //tuples can be indexed
    let five_hundred = x.0;
}

Arrays

Arrays are a group of like-typed values. They are also fixed in size.

fn main() {
    let a: [i32; 5] = [1, 2, 3, 4, 5];

    //shortcut syntax for initializing arrays with some value
    //let b = [0, 0, 0, 0, 0];
    let b = [0; 5];

    //arrays can be indexed
    let first = a[0];
}


CategoryRicottone

Rust/Types (last edited 2021-09-01 19:42:40 by DominicRicottone)