Skip to content

Commit

Permalink
Merge branch 'main' of github.com:gist-rs/book
Browse files Browse the repository at this point in the history
  • Loading branch information
katopz committed Sep 23, 2023
2 parents 15bae9b + fa2d635 commit eb3f187
Show file tree
Hide file tree
Showing 42 changed files with 512 additions and 206 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gist of Rust Book

Focus on gist of `Rust`, `Wasm`, `Solana`.
Focus on gist of `Rust`, `Wasm`, `Solana`, `Generative AI`.

> 🚧 This gist is WIP, feel free to help.
Expand Down Expand Up @@ -35,3 +35,7 @@ If you have any feedback please feel welcome to [open an issue](https://github.c
## Discuss

This repo has a [discussion forum](https://github.com/gist-rs/book/discussions)! Feel free to ask questions or start discussions.

## TODO

- [ ] `WASM`: Try `alloc` from [https://hackmd.io/@alxiong/rust-no-std]()
6 changes: 5 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
- [Enum](rust/r3/enum.md)
- [Derive More](rust/r3/derive-more.md)
- [Typed Builder](rust/r3/typed-builder.md)
- [Generic array](rust/r3/generic-array.md)
- [Generic Array](rust/r3/generic-array.md)
- [AsRef](rust/r3/as-ref.md)
- [Callback](rust/r3/callback.md)
- [Async](rust/r3/async.md)
- [Iterators](rust/r3/iterators.md)
- [R2 - Expert](rust/r2/mod.md)
- [Hello Github Action](rust/r2/hello-github-action.md)
- [Hello Actix CloudRun](rust/r2/hello-actix-cloudrun.md)
Expand All @@ -60,4 +63,5 @@
- [ML](ml/mod.md)
- [Large Language Model](ml/large-language-model.md)
- [Stable Diffusion](ml/stable-diffusion.md)
- [Hugging Face - Candle](ml/huggingface-candle.md)
- [Bye](bye.md)
1 change: 1 addition & 0 deletions src/bye.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
1. [Second State](https://github.com/second-state/wasm-learning)
1. [WebAssembly threads](https://web.dev/webassembly-threads/)
1. [Rhai - WebAssembly (WASM) Build](https://rhai.rs/book/start/builds/wasm.html)
1. [Rust `no_std` Playbook](https://hackmd.io/@alxiong/rust-no-std)

## Binding Resources

Expand Down
Binary file added src/hello/largeindustries_aug23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/hello/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## TL;DR

- ![](/assets/kat.png) Learn <a class="button" href="/hello/why-rust.html">Rust</a>, <a class="button" href="/hello/why-wasm.html">Wasm</a>, <a class="button" href="/ml/mod.html">ML</a>, <a class="button" href="/hello/why-solana.html">Solana</a> ![](/assets/duck.png) or <a class="button" href="/bye.html">More</a>
- ![](/assets/kat.png) <span class="speech-bubble">Learn <a class="button" href="/hello/why-rust.html">Rust</a>, <a class="button" href="/hello/why-wasm.html">Wasm</a>, <a class="button" href="/ml/mod.html">ML</a>, <a class="button" href="/hello/why-solana.html">Solana</a> ![](/assets/duck.png) or <a class="button" href="/bye.html">More</a></span>

## Welcome

Expand All @@ -22,3 +22,7 @@

- We won't cover the topics that we didn't have a chance to use it yet e.g. [`macros`](https://doc.rust-lang.org/book/ch19-06-macros.html), [`unsafe`](https://doc.rust-lang.org/nomicon/),[`FFI`](https://github.com/dtolnay/cxx), [embed device](https://doc.rust-lang.org/stable/embedded-book/).
- We maybe cover `game`, `3d`, `ml` topic if we get enough funding and time on earth.

## Coffee?

- Buy me some ☕️ so I can turn it to code! 👉 <a class="button" href="https://patreon.com/gist_rs" title="Sponsor" aria-label="Sponsor" target="_blank" rel="noopener"><i id="sponsor-button" class="fa fa-heart fa-beat beat-fade"></i> Patreon</a>
6 changes: 6 additions & 0 deletions src/hello/why-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
- Vercel: [`Rust` was a clear choice from day one.](https://vercel.com/blog/turborepo-migration-go-rust)
- Volvo: [Code developed from scratch should definitely be developed in `Rust`.](https://medium.com/volvo-cars-engineering/why-volvo-thinks-you-should-have-rust-in-your-car-4320bd639e09)

## Jobs

> Refer to: [Rust Jobs Report: August 2023](https://filtra.io/rust-aug-23)
![](largeindustries_aug23.png)

## Resources

- [awesome-rust](https://github.com/katopz/awesome-rust)
Expand Down
45 changes: 45 additions & 0 deletions src/ml/huggingface-candle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Hugging Face - Candle

> [https://github.com/huggingface/candle]()
A minimalist ML framework for Rust with a focus on performance (including GPU support) and ease of use.

### Examples

These online demos run entirely in your browser:

- [yolo](https://huggingface.co/spaces/lmz/candle-yolo): pose estimation and
object recognition.
- [whisper](https://huggingface.co/spaces/lmz/candle-whisper): text to speech.
- [LLaMA2](https://huggingface.co/spaces/lmz/candle-llama2): text generation.
- [LLaMA and LLaMA-v2](https://github.com/huggingface/candle/blob/main/candle-examples/examples/llama/): general LLM.
- [Falcon](https://github.com/huggingface/candle/blob/main/candle-examples/examples/falcon/): general LLM.
- [StarCoder](https://github.com/huggingface/candle/blob/main/candle-examples/examples/bigcode/): LLM specialized to code
generation.
- [Quantized LLaMA](https://github.com/huggingface/candle/blob/main/candle-examples/examples/quantized/): quantized version of
the LLaMA model using the same quantization techniques as
[llama.cpp](https://github.com/ggerganov/llama.cpp).

<img src="https://github.com/huggingface/candle/raw/main/candle-examples/examples/quantized/assets/aoc.gif" width="600">

- [Stable Diffusion](https://github.com/huggingface/candle/blob/main/candle-examples/examples/stable-diffusion/): text to
image generative model, support for the 1.5, 2.1, and SDXL 1.0 versions.

<img src="https://github.com/huggingface/candle/raw/main/candle-examples/examples/stable-diffusion/assets/stable-diffusion-xl.jpg" width="200">

- [yolo-v3](https://github.com/huggingface/candle/blob/main/candle-examples/examples/yolo-v3/) and
[yolo-v8](https://github.com/huggingface/candle/blob/main/candle-examples/examples/yolo-v8/): object detection and pose
estimation models.

<img src="https://github.com/huggingface/candle/raw/main/candle-examples/examples/yolo-v8/assets/bike.od.jpg" width="200"><img src="https://github.com/huggingface/candle/raw/main/candle-examples/examples/yolo-v8/assets/bike.pose.jpg" width="200">

- [segment-anything](https://github.com/huggingface/candle/blob/main/candle-examples/examples/segment-anything/): image
segmentation model with prompt.

<img src="https://github.com/huggingface/candle/raw/main/candle-examples/examples/segment-anything/assets/sam_merged.jpg" width="200">

- [Whisper](https://github.com/huggingface/candle/blob/main/candle-examples/examples/whisper/): speech recognition model.
- [Bert](https://github.com/huggingface/candle/blob/main/candle-examples/examples/bert/): useful for sentence embeddings.
- [DINOv2](https://github.com/huggingface/candle/blob/main/candle-examples/examples/dinov2/): computer vision model trained
using self-supervision (can be used for imagenet classification, depth
evaluation, segmentation).
3 changes: 2 additions & 1 deletion src/ml/mod.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ML

![](/assets/kat.png) A notes about ML, still ongoing!
![](/assets/kat.png) <span class="speech-bubble">A notes about ML, still ongoing!</span>

## Goals

Expand All @@ -10,3 +10,4 @@

- [Large Language Model](large-language-model.md)
- [Stable Diffusion](stable-diffusion.md)
- [Hugging Face - Candle](huggingface-candle.md)
6 changes: 4 additions & 2 deletions src/rust/mod.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Rust

![](/assets/kat.png) Welcome to `Rust` journey. I'll be with you til the end. Enjoy!
![](/assets/kat.png) <span class="speech-bubble">Welcome to `Rust` 🦀 journey. I'll be your companion til the end!</span>

![](/assets/duck.png) <span class="speech-bubble">Cool!</span>

## R5 : Entry

> An easy parts of `Rust` that you need for surviving chewing glass.
> An easy parts of `Rust` that you need for survival.
[Enjoy R5 ➠](./r5/mod.md)

Expand Down
4 changes: 2 additions & 2 deletions src/rust/r1/marker-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Marker Types

![](/assets/kat.png) Let's make use of `Marker Types` by a little help from [PhantomData](https://doc.rust-lang.org/nomicon/phantom-data.html) which use for make compiler happy.
![](/assets/kat.png) <span class="speech-bubble">Let's make use of `Marker Types` by a little help from [PhantomData](https://doc.rust-lang.org/nomicon/phantom-data.html) which use for make compiler happy.</span>

```rust,editable
use std::marker::PhantomData;
Expand Down Expand Up @@ -50,7 +50,7 @@ fn main() {
let ps5 = PlayStation::default();
// Can access only version.
println!("ps4_version:{}", ps5.version());
println!("ps5.version:{}", ps5.version());
// Can play after turn on.
ps5.turn_on().play();
Expand Down
4 changes: 2 additions & 2 deletions src/rust/r1/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

> 🚧 UNDER CONSTRUCTION: Please bear 🧸 with me! If you can't wait please try [read more](../../bye.md).
![](/assets/kat.png) Welcome to highest curve of learning `Rust`, be proud of yourself!
This is a last part so you can expect super confuse and hard to understand topic. Let's roll!
![](/assets/kat.png) <span class="speech-bubble">Welcome to highest curve of learning `Rust`, be proud of yourself!
This is a last part so you can expect super confuse and hard to understand topic. Let's roll!</span>

## Goals

Expand Down
2 changes: 1 addition & 1 deletion src/rust/r2/hello-github-action.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hello Github Action

![](/assets/kat.png) Here's how we release `Rust` library with built binary via `Github Action` (rn with some caveat).
![](/assets/kat.png) <span class="speech-bubble">Here's how we release `Rust` library with built binary via `Github Action` (rn with some caveat).</span>

## Release

Expand Down
2 changes: 1 addition & 1 deletion src/rust/r2/mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> 🚧 UNDER CONSTRUCTION: Please bear 🧸 with me! If you can't wait please try [read more](../../bye.md).
![](/assets/kat.png) At this point you should familiar with your/other library and want to expose your lib/service to the world. Let's do it!
![](/assets/kat.png) <span class="speech-bubble">At this point you should familiar with your/other library and want to expose your lib/service to the world. Let's do it!</span>

## Goals

Expand Down
118 changes: 118 additions & 0 deletions src/rust/r3/async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Async

> 🤔 refer to [The State of Async Rust](https://corrode.dev/blog/async/)
## The State

```mermaid
graph LR;
A("Async madness")
A-.Try to cover Rust standard.->C("async-std<br/>abandoned 😱")
A-.Try to cover any runtime.->B("Tokio<br/>Multi threaded by default = complex 🫠")
```

## Async vs Threads

### Threads

```rust,editable
use std::error::Error;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::{thread, time};
// This fn could be used by both async and sync callers,
// eliminating the need for an asynchronous runtime.
fn read_contents<T: AsRef<Path>>(file: T) -> Result<String, Box<dyn Error>> {
let mut file = File::open(file)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
return Ok(contents);
}
fn main() {
thread::scope(|scope| {
// worker thread 1
scope.spawn(|| {
let contents = read_contents("foo.txt");
// do something with contents
});
// worker thread 2
scope.spawn(|| {
let contents = read_contents("bar.txt");
// ...
});
// worker thread 3
scope.spawn(|| {
let contents = read_contents("baz.txt");
// ...
});
});
// Threads get joined automatically
}
```

### Async

```rust
#[tokio::main]
async fn main() {
// This will print a warning, but compile and do nothing at runtime
read_contents("foo.txt");
// futures do nothing unless you `.await` or poll them
}
```

> 🤔 refer to [Asynchronous Programming in Rust](https://rust-lang.github.io/async-book/01_getting_started/02_why_async.html#async-vs-threads-in-rust)
![](/assets/kat.png) <span class="speech-bubble">If you don't need async for performance reasons, threads can often be the simpler alternative.</span>

## Best practices

1. `synchronous` by default.
1. opt into `async` as needed.
1. less main with `#[tokio::main]`.

```rust,no_run
#[get("/")]
fn index() -> impl Response {
let users = db::get_users();
Response::ok().body(render_template("index.html", users))
}
// A route, which profits from concurrent IO
// It sends multiple requests to an external API and aggregates the results
// Note how the function itself is does not need to be async
#[get("/users")]
fn users(count: usize) -> impl Response {
// Start a local, single-threaded runtime with smol's async-executor
let rt = smol::LocalExecutor::new();
// Run the async code on the runtime
let results = rt.run(async {
let mut results = Vec::new();
for id in 0..count {
let result = reqwest::get(format!("https://api.example.com/users/{}", id)).await?;
results.push(result);
}
Ok(results)
});
Response::ok().body(render_template("users.html", results))
}
// This does not need to be async either!
// In the background, it might use a thread pool to handle multiple requests
fn main() -> Result<()> {
let app = App::new()
.mount("/", index)
.mount("/users", users)
.run();
}
```

![](/assets/kat.png) <span class="speech-bubble">Read more 👉 [Asynchronous Programming in Rust](https://rust-lang.github.io/async-book/01_getting_started/01_chapter.html)</span>
Loading

0 comments on commit eb3f187

Please sign in to comment.