# Rust Learning Notes ## Overview <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Rust_programming_language_black_logo.svg/1024px-Rust_programming_language_black_logo.svg.png" align="right" width="100" height="100" /> A collection of notes gathered from exploring the low-level programming language `rust`. Rust is a systems programming language that has support for OOP programming but focuses on the functional paradigm. Systems programming focuses on high performant code ex: C/C++. When every cycle counts we use systems programming. Rust has a strong compiler that can detect race conditions ahead of time, and has a strong focus on type-safety. ## Structure In regards to structure rust can be quite tricky. Rust seems to require the use of `mod.rs` files. Say you have the following situation. You have `main.rs`, and you have a sub-folder with the name `logic/`. If you want to use any mod within the sub-folder then you have to do the following. * In `logic/` create a file `mod.rs` * In `mod.rs` add all your other mods * In `main.rs` add `mod logic;` * Finally in `main.rs` add `use crate::logic::*` ## Cargo * Rust uses cargo as package management. * Cargo is useful for creating projects + maintenance. ### Commands * Compile artifacts + code ` cargo build ` * Remove artifacts ` cargo clean ` * Run compiled executable ` cargo run ` * Update project dependencies ` cargo update ` this will only step the latest patch-version. I.e the 'z' in the following example. ` dep.x.y.z` * Unit tests in rust natively don't display any print logs. To toggle this simple run the following `cargo test -- --nocapture` ## Rust ### Naming Conventions * Variables & Functions: `snake_case` * Traits & Structs: `PascalCase` ### Attributes You will quickly stumble upon a thing in rust known as **attributes**. Hera re a few examples: * `#![allow(non_snake_case)]` * `#[allow(unused)]` * `#[cfg(test)]` ### Variables * Are always immutable unless specified as mut. * There is a difference between constants and variables. ### Functions * Return values are declared in the following manor: ```rust fn get_five() -> i32 ``` ### Comments * C-Styled commenting, i.e `// my comment`. ### Control Flow * You can define one line if in the following manor: ``` rust let number = if condition { 5 } else { 6 }; ``` ### Loops * Can be written in multiple ways. `loop`, `while`, `for`. * To return a value from a loop you must `break 'x'`. Here we break and return the value of x. ### Modules * Written by `mod foo {}` * If a module named foo has no submodules, you should put the declarations for foo in a file named foo.rs. * If a module named foo does have submodules, you should put the declarations for foo in a file named foo/mod.rs. ### Structs * Written by `struct foo {}` * Structs are used as data containers, unlike C/C++ they contain methods. * Struct methods are handled within a struct `impl foo {}` * This does provide a neat way of handling data structures, while also holding onto the functional programming aspect of Rust. ### Domain Types In functional programming domain types are quite common to see. They give the api, a specific way of working, that should not surprise the programmer. * `struct Uri(uri: String);` ### Borrow Checker #todo ### Lifetimes #todo ## Links : * [Better docker/rust deployments](https://shaneutt.com/blog/rust-fast-small-docker-image-builds/) * [Docker/Rust building a basic docker app](https://blog.knoldus.com/containerize-rust-application-with-docker/)