# Rust Notes ###### tags: `Internal` ### Packages are used to build, test, and share crates. They can contain multiple crates. ### Crates are modules that product a library or executable (a file with code in it) ### Modules organize code and handle privacy. A module is basically a container for related code. Modules can be nested inside other modules. ### Module Scope if you have a module inside another module, you can use the `super` keyword. Ex: ```rust mod Restaurant { pub struct Pizza { // } mod help_customer { fn take_order(){ let pizza: super::Pizza = /// } } } ``` ### Public vs Private everything is private by default. To make something public, it's just prefaced with `pub` ### Vectors a vector is a special type available through the std Rust library. It is essentially just an array but with a dynamic size. A standard array always has a fixed size. Vectors have built-in pop and push functions (among others). ### Tuples tuples looks like this: (val1, val2, val3) ### References if you don't use a reference and assigned a variable (A) to another variable (B), it will delete (?) the first variable (A). If you use a reference, it won't change the first variable ### Cloning to make a copy of something you can use the clone method ```rust let string1 = String::from('"Hello"); let string2 = string1.clone() ``` ### Unwrapping You can use the unwrap() method to get the data returned from something that first returns the status if successfull, and then returns the data. Example: ```rust // With unwrap: let output: File = File::create(path).unwrap() // Without unwrap: let output: Result<File, Error> = File::create(path); let mut output: File = match output { Ok(file: File) => file, Err(error: Error) => { panic!("Problem creating file: {}", error); } } ``` ### Ownership - each value has a variable that's called it's owner - there is only one owner at a time - when the owner goes out of scope the value disappears (in memory) ### Stack stores values in a last-in first-out format data must have a defined size ### Heap you request a certain amount of space in memory, and get a pointer which is a reference to that space in memory ### Structs structs are like interfaces in Typescript, and you can also make them generics: ```rust struct Rectangle<T, U> { length: T, height: U, } let rec: Rectangle<i32, f64> = Rectangle {length: 4, height: 10.5} ``` You can also implement structs: ```rust struct Pizza { dough: String, cheese: String, topping: String } impl Pizza { fn lunch(topping: &str) -> Pizza { return Pizza { dough: String::from("regular dough"), cheese: String::from("mozz"), topping: String::from(topping) } } } ``` ### Traits traits are like interfaces/structs but are for the prototype functions of a variable. Traits need to be implemented with impl to be used. Example: ```rust trait Shape { fn new(length: f32, width: f32) -> Self; fn area(&self) -> f32; } struct Rectangle {length: f32, width: f32}; impl Shape for Rectangle{ // this is the constructor function fn new(length: f32, width: f32) -> Rectangle { return Rectangle{length, width} } fn area(&self) -> f32 { return self.length * self.width; } } let rec: Rectangle = Shape::new(length: 10.0, width: 10.0); println!("Rectangle Area: {}", rec.area()); ``` ## Error Handling ### panic can use `panic!("error message!");` to throw error ### expect can use `.expect(msg: "error message")` to handle errors ### kinds You can look for specific types of errors ```rust let output: Result<File, Error> = File::create(path); let mut output: File = match output { Ok(file: File) => file, Err(error: Error) => match error.kind() { ErrorKind::NotFound => {// file not found, could make a new file here} } } ``` You can use Rust and the fuels Rust SDK to test your contract. Rust is a general purpos language, while Sway is tailored specifically for the FuelVM environment, so a lot of Rust features are not available in Sway. Because Sway is so closely related to Rust, you can follow a lot of the same syntax and patterns that you've learned already.