Skip to content

mhnaufal/rust-book

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 Nothing special here!

Inspired from

📌 Rust Book

📌 Easy Rust

📌 A half hour to learn Rust

Shortcuts

  1. Variable 🔰 Book
  • Rust variable immutable by default
  • To shadowing a variable, declare a variable with the same name with the one that we want shadowed
let point = 32;

// shadowing
let point = 23.1;
  • Casting data type can be done by using the as keyword
let point = (32 as f32);
  1. Data type 🔰 Book
  • We usually to declare the data type of a variable explicitly (Type annotation)
  • Rust allows to use underscore _ in numbers to make read the code easier
  • Rust will automatically call panic if integer overflow happen, but when compiling with release mode, Rust will not call panic, instead it will performs two's complement wrapping
  • Tuple holds a bunch of elements with different types
  • Array holds a bunch of elements with the same type
  1. Function 🔰 Book

  2. Control Flow 🔰 Book

  3. Ownership 🔰 Book

  4. Borrowing 🔰 Book

  5. Struct 🔰 Book

  6. Enum 🔰 Book

  7. Vector 🔰 Book

  8. String 🔰 Book

  9. HashMap 🔰 Book

  10. Error 🔰 Book

  • Rust has two type of error, the recoverable one using Result<T, E> enum and unrecoverable using panic!
  • When panic! happen, Rust will do unwinding or walks back up the stack and cleans it
  1. Generic 🔰 Book
  • Generics are abstract stand-ins for concrete types or other properties
fn largest<T>(list: &[T]) -> T {
  • The function largest is generic over some type T. This function has one parameter named list, which is a slice of values of type T. The largest function will return a value of the same type T.

  • Usually combined with Trait and Lifetime

  1. Trait 🔰 Book
  • Trait are similiar to interface in other programming language
  • Declare a function/method/implementation name without giving the body function
  • Trait can also has default implementation
  • Function can also take type of trait for their parameter(s)
  1. Lifetime 🔰 Book
  • Every refence has a lifetime
  • Most of the time, lifetime are implicit, like type. However, we must annotate lifetime when lifetime references could be related in multiple ways.
  • It's only about Scope
  • Syntax => 'a, &'b
  1. Test 🔰 Book
  • Rust provide built-in functionallity for testing, whether unit testing or even integration testing
  • Keywords used, assert, assert_eq, assert_ne
  • assert will evaluates to boolean
  • We can add a custom error failed test message
  • We can use Result<T, E> type in test
  • Tests are run parallel or consecutively by default
  • In order to run in non parallel give the number of threads with --test-threads=1 flag
  • We also can provide the test function name in order to run the particular test and there is also an #[ignore] macro
  • Integration test has already provided default by Rust
  1. Functional Programming 🔰 Book
  • Rust provide clousre or function as a varible / first class citizen
  • Use the pipe symbol | | to create a parameters for the closure
  • Closure can be use to catch the environment of the closure as long its in the same block
  • Iterator in Rust is implemented in Trait

Projects

List of me trying create a project in Rust

Link to HERE!

Run

cargo run main.rs

Module tree

cargo modules generate tree
cargo modules generate tree --with-types

About

Rust (close to) No Zero Day & Nothing to Rust

Topics

Resources

Stars

Watchers

Forks

Languages