Did you remember everything I just said?

Of course not. Learning a language that is not so different from yours is nice, but unless you put it into practice, you will literally forget everything.


We will be putting these into practice, i just wanted to get through this as fast as possible to get to the fun parts.

















Basics on Rust

Just some basics so we can understand things going forward


When you are starting out using rust you should see

  1. unwraps
  2. clones

That is totally normal, completely fine. understanding, at least for me, comes in waves. The more I understand, the more I realize I understand less.

















Lets start learning! Starting with a Vector

Vec (rust) and [] (typescript) are very similar. Their behaviors are near identical.

these two are functionally equivalent

const a = [1, 2, 3, 4, 5] as const;
let a = vec![1, 2, 3, 4, 5];
















Mutation

const a = [1, 2, 3, 4, 5];
a.push(6); // [1, 2, 3, 4, 5, 6] // returns size
let a = vec![1, 2, 3, 4, 5];
a.push(6); // Error: a is not mutable

// but with rust we can shadow
let mut a = a;
a.push(6); //  [1, 2, 3, 4, 5, 6] // does not return size
const a = [1, 2, 3, 4, 5];
a.pop(); // [1, 2, 3, 4] undefined or T
let mut a = vec![1, 2, 3, 4, 5];
a.pop(); //  [1, 2, 3, 4] Option<T>
















Accessing Data

const a = [1, 2, 3, 4, 5] as const;
const item = a[2];
let a = vec![1, 2, 3, 4, 5];
let item = a[2]; // does work, but if out of bounds, panic
let item = a.get(2); // better, returns Option<T> where T can be i32

An Option<T> is a possible undefined value. All things could be undefined, must be specified with an Option

we will talk about enums and Options in depth shortly

















Gentle Reminder: Now you will forget everything we just said

But to help you not forget, we will start doing some exercises soon.

















Tuple

This doesn't really have a similarity in javascript.

let a = (5, String::from("hello")); // this type is (i32, String)

it is "near" equivalent to

const a = [5, "hello"];

You can pattern match (think destructuring) tuples.

let a = (5, String::from("hello")); // this type is (i32, String)

// you probably best know this as destructuring, but we will refer to this
// as pattern matching.
let (my_num, my_str) = a;

You can even pattern match in a function

let a = (5, String::from("hello")); // this type is (i32, String)

// you can even pattern match on functions.
fn bar((my_num, my_str): (i32, String)) {
}

bar(a);




Structs

struct MyStruct {
    x: usize,
    y: usize,
    z: usize,
}

fn bar(MyStruct { y, z, .. }: MyStruct) -> bool {
    return y * z < 100;
}

fn main() {

    let foo = MyStruct {
        x: 69,
        y: 420,
        z: 1337,
    };

    let MyStruct { x, .. } = foo;
    let MyStruct { y, z, .. } = foo;
    let MyStruct { x, y, z } = foo;

    if let MyStruct { x, .. } = foo {
        println!("things about x {}", x)
    }

}

You can solve such crazy problems with pattern matching

















unwrap, todo, and unreachables

These are things that only exist within Rust and may be a bit confusing.

















TODO:

I particularly like this as it allows for me to do // TODO: but i am forced to handle it in rust

Lets show an example

(code time)

















Unreachable

This unsures that the program behaves properly.

think of it like an assert statement.

lets do a quick example

(code time)

















Unwrap

when you are new to rust, unwraps are your friend. You will end up using them a lot because you don't know the language / tools available to you yet. Often you feel like you don't know how to handle some basic operations and unwrap can be your friend.

You learn by doing, you learn better by doing wrong

But!!! if you unwrap an Error or an undefined, your program crashes... so... don't do it.

Let me give you a quick example of that.