Skip to content

Commit

Permalink
fix: typo
Browse files Browse the repository at this point in the history
  • Loading branch information
katopz committed Sep 24, 2023
1 parent eb3f187 commit 0da2c2c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/rust/r5/enjoy1.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ You will know when and which to use it later. Let's just use it for now.</span>
```rust,editable
fn main() {
// Convert &str to String
let foo_str = "foo"; // &str
let foo_string = foo_str.to_string(); // String
let foo_str = "foo"; // &str 👈🤯 This weird & will make sense later.
let foo_string = foo_str.to_string(); // String 👈 So we can move it.
println!("foo_str: {foo_str}");
println!("foo_string: {foo_string}");
Expand All @@ -187,7 +187,7 @@ fn main() {
assert_eq!(bar_string, foo_str.to_string());
// But if you really want to keep access `foo_string`.
// Just don't move in the first place! See below👇
// Just don't move it in the first place! See below 👇
// 1️⃣ let other borrow `&` instead of move.
let borrowed_bar_string = &bar_string;
Expand All @@ -202,7 +202,7 @@ fn main() {
```

![](/assets/kat.png) <span class="speech-bubble">So we need `&` to borrow the instead of moving it.
Anyway we tend to avoid `clone`/`copy` to reduce overhead aka [zero-copy](https://swatinem.de/blog/magic-zerocopy) as possible.</span></span>
Anyway we tend to avoid `clone`/`copy` to reduce overhead aka [zero-copy](https://swatinem.de/blog/magic-zerocopy) as possible.</span>

---

Expand Down
22 changes: 11 additions & 11 deletions src/rust/r5/enjoy2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

```rust,editable
fn main() {
// Create new `vec` and `array` of `&str`.
let mut vec_of_foo = vec!["foo", "bar"]; // Say hi to vec! macro.
// Create new `array` of `&str` and `vec`.
let array_of_foo = ["foo", "bar"]; // Array of &str.
let mut vec_of_foo = vec!["foo", "bar"]; // Say hi to vec! macro.
println!("vec_of_foo: {vec_of_foo:#?}");
println!("array_of_foo: {array_of_foo:#?}");
println!("vec_of_foo: {vec_of_foo:#?}");
// The different?
vec_of_foo.push("baz"); // You can push more to Vec
// 😱 Uncomment to see an error "no method named `push` found for array `[&str; 2]`".
// FYI: `[&str; 2]` mean fixed array of &str usize 2.
// 👍 Anyway fixed size is actually good for memory management, don't hate it!
// 👍 Anyway fixed size is good for memory management, so don't hate it!
// array_of_foo.push("baz"); // You can't to fixed Array [&str; 2]
// 1️⃣ Back to Vec, Let's iterate them.
Expand Down Expand Up @@ -60,17 +60,17 @@ fn main() {
}
```

> 💡 `::<>` is [turbo fish](https://turbo.fish/) <span style="transform: scaleX(-1);"><span>🐟💨</span></span> .
> 💡 Read more about `iterate` [here](https://doc.rust-lang.org/rust-by-example/trait/iter.html).
> 💡 `::<>` is [turbo fish](https://turbo.fish/) <span style="transform: scaleX(-1);"><span>🐟💨</span></span> .
> 💡 Read more about `iterate` [here](https://doc.rust-lang.org/rust-by-example/trait/iter.html).
> 💡 If you crazy about `iterate` do try [Rust Iterator Cheat Sheet](https://danielkeep.github.io/itercheat_baked.html)
![](/assets/kat.png) <span class="speech-bubble">So `iter` will make an auto borrow `&` for us which is handy.
![](/assets/kat.png) <span class="speech-bubble">So `iter` will make an auto borrow `&` for us which is handy.
And we can use `into_iter` if we need.</span>

![](/assets/duck.png) <span class="speech-bubble">Also `<Vec<_>>` is for lazy crab like us, nice!</span>

> 🏂 Fun fact!
> `String` and `Vec` is allocated in `heap`.
> 🏂 Fun fact!
> `String` and `Vec` is allocated in `heap`.
> `str` and `array` is allocated in `stack`.
### HashMap, match, Option, Some, None, unwrap_or, panic
Expand Down Expand Up @@ -142,10 +142,10 @@ fn main() {
}
```

> 💡 `T` is generic, `unwrap` always has 2 outputs.
> 💡 `T` is generic Type, `None` is nothing, `unwrap` always has 2 outputs.
```
╭─▶︎ Some<T>
╭─▶︎ Some(T)
Option<T> ── unwrap ──┤
╰─▶︎ None
```
Expand Down
6 changes: 3 additions & 3 deletions src/rust/r5/enjoy3.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Enjoy Day 3

![](/assets/kat.png) <span class="speech-bubble">Hey! Nice to see you here. Previously we use `HashMap` which is fine but `Struct` is way more better, Let's get grab some coffee ☕️ and getting start.</span>
![](/assets/kat.png) <span class="speech-bubble">Hey! Nice to see you here. Previously we use `HashMap` which is fine but `Struct` is way more better, Let's grab some coffee ☕️ and getting start.</span>

## Struct

```rust,editable
fn main() {
// 😭 Before: use `Tuple`.
// 😭 Before: we did use `Tuple`.
let animal = (("name", "foo"), ("age", 42));
println!("{0:?}: {1:?}", animal.0 .0, animal.0 .1); // 😭 So hard to access tuple!
println!("{0:?}: {1:?}", animal.1 .0, animal.1 .1); // 😭 Stop this!
// 😚 After: use `Struct`.
// 😚 After: we use `Struct` instead.
struct Animal {
name: String, // We use `String` here not &str (will talk about this later).
age: u8, // `u8` mean unsigned integer (2^8 − 1) = 255
Expand Down
9 changes: 8 additions & 1 deletion src/rust/r5/enjoy4.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ fn main() {

</details>

> 💡 `Result<T, E>`⎯⎯ unwrap → `Ok<T>`/`Err(E)` which `T`,`E` are generic.
> 💡 `T` is generic Type, `E` is generic Error, `unwrap` always has 2 outputs.
```
╭─▶︎ Ok(T)
Result<T, E> ── unwrap ──┤
╰─▶︎ Err(E)
```

> Read more about how to handle `Result` [here](https://doc.rust-lang.org/rust-by-example/error/result.html)
---
Expand Down
2 changes: 1 addition & 1 deletion src/rust/r5/enjoy5.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enjoy Day 5

![](/assets/kat.png) <span class="speech-bubble">We `impl` (implement) some `trait` (aka skill) for `struct` so that `struct` can have that skill.</span>
![](/assets/kat.png) <span class="speech-bubble">We `impl` (implement) some `trait` (aka skill) for our `struct` so it can have any desired skill.</span>

## trait, impl

Expand Down
4 changes: 2 additions & 2 deletions src/rust/r5/teardown.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
1. Generic `T` and `E` nearly like generic in `TypeScript` so it should be easy there.
1. Use `iter`, `iter_into`, `collect` wisely, but no worry [`clippy`](https://doc.rust-lang.org/clippy/) will got your back anyway.
1. Choose `composition` over `inheritance`, learn to love `struct`, `impl`, `trait`, `derive` instead.
1. We `impl` (implement) some `trait` (aka skill) for `struct` so that `struct` can have that skill.
1. We `impl` (implement) some `trait` (aka skill) for `struct` so it can have that skill.
1. `String`,`Vec`,`Box` are smart pointer allocated on `heap`.
1. `str`, `array`, `struct`, and other primitives type are allocated on `stack`.
1. Compiler will ask to add `dyn` when need e.g. `Box`, `Supertraits`.
1. Compiler will ask to add `dyn` when needed e.g. `Box`, `Supertraits`.
1. `Dynamic Dispatch` (Box) can be replace with `Static Dispatch` if need.
1. Don't over thinking! Do trust `clippy` and 🦀[`Rustacean`](https://rustacean-principles.netlify.app/) and you will be fine.
1. Did we forget `Some(thing)`? 🤔
Expand Down

0 comments on commit 0da2c2c

Please sign in to comment.