Tear down
Recap
-
🦀
Rust
like to move it move it 🎵, borrow&
when needed. -
🦀
Rust
variable areimmutable
by default,mut
when needed. -
🦀
Rust
variable will drop when out of scope{ }
, consider borrow&
when needed. -
No
null
, onlyOption
, Usematch
(or other ways) to handle it;unwrap
andexpect
willpanic
.#![allow(unused)] fn main() { enum Option<T> { Some(T), None, } }
-
When
fn
returnResult
, Usematch
(or other ways) to handle it;unwrap
andexpect
willpanic
.#![allow(unused)] fn main() { enum Result<T, E> { Ok(T), Err(E), } }
-
Both
Option
,Result
areenum
so eat that frog 🐸! -
Generic
T
andE
nearly like generic inTypeScript
so it should be easy there. -
Use
iter
,into_iter
,collect
wisely, but no worryclippy
will got your back anyway. -
Choose
composition
overinheritance
, learn to lovestruct
,impl
,trait
,derive
instead. -
We
impl
(implement) sometrait
(aka skill) forstruct
so it can have that skill. -
String
,Vec
,Box
are smart pointer allocated onheap
. -
str
,array
,struct
, and other primitives type are allocated onstack
. -
Compiler will ask to add
dyn
when needed e.g.Box
,Supertraits
. -
Dynamic Dispatch
(Box) can be replace withStatic Dispatch
if need. -
Don't over thinking! Do trust
clippy
and 🦀Rustacean
and you will be fine. -
Did we forget
Some(thing)
? 🤔
Easy right? We're not done yet. Let's dig deeper! 👉 Continue to R4 ➠