{%hackmd C-_zW0vxSXSSsOkJHknc8g %}
# Rust 的奇幻旅程 <br>
## 一切就是從這裡開始的
[TOC]
----
Made by FanRende (030Mortal#5525)
<img src="https://i.imgur.com/p6FrmOj.png" width="40%">
---
## 開發環境
在 Arch Linux 的一個 distribution Manjaro 上開發
Local 時使用 VSCode 及 terminal
Remote 時使用 [Code Server](https://github.com/coder/code-server)
搭配 ssh forward local port
---
## 參考教學
- [The Rust Programming Language](https://doc.rust-lang.org/book/)
- [非官方翻譯](https://rust-lang.tw/book-tw)
---
## Install Rustup
```shell
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
```
----
### This will also install
- `rustc`: Rust 的編譯器
- `rustup`: Rust、Cargo 的版本管理工具
- `rust-gdb`: GDB for Rust
- `rust-gdbgui`: GUI GDB
- `rust-lldb`: LLVM Debugger for Rust
- `rustfmt`: Rust 官方的 formatter
- `rustdoc`: Document 的生成工具
- 前提是要有寫正確的註解
---
## Hello World
```rust
// main.rs
fn main() {
println!("Hello, world!");
}
```
```shell
$ rustc main.rs
$ ./main
Hello, world!
```
---
## Hello Cargo
<div class="flex-container">
<div class="flex-content">
```shell
$ cargo new hello_cargo
```
```shell
$ tree hello_cargo
hello_cargo
├── Cargo.lock
├── Cargo.toml
├── .git
└── ...
├── .gitignore
└── src
└── main.rs
```
</div>
<div class="flex-content">
```toml
# Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more ...
[dependencies]
```
</div>
</div>
----
### Cargo commands
- `cargo new`: 新建專案
- `cargo build` / `cargo b`: 建置專案
- `cargo run` / `cargo r`: 執行專案
- `cargo check` / `cargo c`: 建置但不產生執行檔
- `cargo clean`: 刪除 target 資料夾
- `cargo doc` / `cargo d`: 產生專案 Doc
---
## Guessing Game

----
- Cargo 專案
- `use std::io`
- `stdin().read_line().except()`
- immutable & mutable variable
- library crate
- random number generating
- `use std::cmp::Ordering`
- `match` expression
- `loop` expression
- `match` expression with return value
----
### Immutable & mutable variable
- 提高安全性及效率
- 共享記憶體導致的錯誤
- 減少 side effect
- concept from functional programming
- 不完全禁止 Mutable variable 以提供彈性
----
### Library crate
- 封裝及分享程式碼
- 可以被其他 Rust 程式引用
- 類似 python 中的 module
- 可以被上傳到 crate.io 等 public registry
----
### `match` expression
- 用來比對數值
- 由多個 pattern arm 組成
- 當數值符合 pattern 就會執行 arm
- 一個數值可以匹配多個 pattern 進而執行多個 arm
- 順序會影響執行結果
- e.g.
```rust
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("You win!"),
}
```
----
### `match` expression with return value
- match 可以用來在 pattern 匹配時回傳特定值
- e.g.
```rust
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
```
---
參考 / 延伸閱讀:
- [學習 Rust](https://www.rust-lang.org/zh-TW/learn)
- [The Rust Programming Language](https://doc.rust-lang.org/book/)
- ChatGPT
<div class="flex-container">
<div class="flex-content">
</div>
<div class="flex-content">
</div>
</div>
<style>
.gray {
color: gray;
font-size: 0.5em;
}
.slides .rust {
font-size: 0.75em !important;
line-height: 1.2em !important;
}
.mermaid {
background-color: rgba(1, 1, 1, .2) !important;
}
.slides code {
background-color: #444 !important;
border-radius: 10px;
white-space : pre-wrap !important;
padding-right: 0.1em;
padding-left: 0.1em;
}
.code-wrapper code {
background-color: inherit !important;
border-radius: inherit;
}
.flex-container {
display: flex;
justify-content: center;
}
.flex-content {
flex-grow: 1;
}
</style>
<style>
/* Customize website's scrollbar like Mac OS */
::-webkit-scrollbar {
-webkit-appearance: none;
width: 7px;
}
::-webkit-scrollbar-thumb {
border-radius: 4px;
background-color: rgba(128, 128, 128, 1);
-webkit-box-shadow: 0 0 1px rgba(255, 255, 255, .5);
}
</style>
{"metaMigratedAt":"2023-06-17T23:52:33.003Z","metaMigratedFrom":"YAML","title":"Rust 的奇幻旅程 一切就是從這裡開始的","breaks":true,"description":"一切就是從這裡開始的","slideOptions":"{\"theme\":\"dark\",\"transition\":\"fade\",\"previewLinks\":true}","contributors":"[{\"id\":\"82f6b599-31b8-4112-9dc5-7d7b7d6a3ebb\",\"add\":5210,\"del\":944}]"}