DEV Community

Cover image for [WIP] Rust - NOTES
Mauricio Reatto Duarte
Mauricio Reatto Duarte

Posted on • Originally published at mauriciord.dev on

[WIP] Rust - NOTES

Tuple

let numbers: (i32, i32, f64) = (1, 2, 3.5);

// destructuring
let (a, b, c) = numbers;

// mutable
let mut mau = (10, 11, 12)
mau.0 = 90 // (90, 11, 12)

mau = (90, 91, 92) // pattern matching

Enter fullscreen mode Exit fullscreen mode

need to respect the type to avoid mismatched types

Array

let neo: [i32;3] = [1, 2, 3]
neo[1] // 2

let mut morpheu = [1.1, 2.2, 3.3]
morpheu[2] = 10.15

// slice
&neo[1..] // [2, 3]
&neo[..2] // [1, 2]
&neo[1..2] // [2]

Enter fullscreen mode Exit fullscreen mode

arrays should have only one type

Out-of-bound error

  • Trying to get an element out of the limits: index out of bounds

Memory Awareness

  • STATIC memory needs fixed-size
static _Y: u32 = 13

Enter fullscreen mode Exit fullscreen mode
  • STACK are local variables,
static _Y: u32 = 13

fn main() {
    let x = 5;
    let z = true;
    let numbers = [1, 2, 3];
}

Enter fullscreen mode Exit fullscreen mode
  • HEAP memory
static _Y: u32 = 13

fn main() {
    let x = 5;
    let z = true;
    let numbers = [1, 2, 3];

    let users = get_users();
}

Enter fullscreen mode Exit fullscreen mode

Memory Cleanup

  • drop method to clean up it

Top comments (0)