- 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);
- 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
- 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
- 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
- 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)
- 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
- 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
- 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
List of me trying create a project in Rust
cargo run main.rs
cargo modules generate tree
cargo modules generate tree --with-types