# Crate
## Week 0E
----
## Introduction
+ Crate 就像是模組
+ 分成 Binary 與 Library 兩種
+ Package 會包含多個 Crate
+ Rust 對模組的權限定義相當嚴格
+ 非常講究模組層級的關係
----
## `mod` & `use` & `pub`
+ `mod` 為模組 Module 的縮寫,用途有兩種:
1. 宣告模組
2. 引用模組
+ `use` 主要用來簡化模組路徑
+ `pub` 用來表示項目是否公開
----
## 固定路徑
+ 預設主程式放在 `src/main.rs`
+ 預設函式庫放在 `src/lib.rs`
+ Crate 名稱在 `Cargo.toml` 裡面定義
----
## 最簡單的形式
直接放在主程式上面
```rust=
// src/main.rs
mod foo {
pub fn bar() {
println!("bar()")
}
}
fn main() {
foo::bar();
}
```
----
## 放在 `lib.rs` 裡面
```rust=
// src/lib.rs
pub mod hello_mod {
pub fn hello_fn() {
println!("hello!")
}
}
```
```rust=
// src/main.rs
fn main() {
hello_crate::hello_mod::hello_fn();
}
```
----
## 使用 `use` 簡化呼叫路徑
```rust=
// src/main.rs
fn main() {
// 不簡化
hello_crate::hello_mod::hello_fn();
// 簡化一階
use hello_crate::hello_mod;
hello_mod::hello_fn();
// 簡化兩階
use hello_crate::hello_mod::hello_fn;
hello_fn();
}
```
----
## 在 `src` 底下放個檔案
`mod` 拿來「宣告」
```rust=
// src/konosuba.rs
pub mod yunyun {
pub fn attack() {
println!("Light of Saber!");
}
}
```
`mod` 拿來「匯入」
```rust=
// src/main.rs
mod konosuba;
fn main() {
konosuba::yunyun::attack();
}
```
----
## 把模組放在資料夾裡面
檔名必須要是 `mod.rs`
```rust=
// src/eris/mod.rs
pub mod skill {
pub fn steal() {
println!("Steal!");
}
}
```
```rust=
// src/main.rs
mod eris;
fn main() {
eris::skill::steal();
}
```
----
## 使用模組裡面的子模組
```rust=
// src/eris/kazuma.rs
pub fn skill() {
println!("Steal Pants!")
}
```
```rust=
// src/eris/mod.rs
pub mod kazuma;
```
```rust=
// src/main.rs
mod eris;
fn main() {
eris::kazuma::skill();
}
```
----
## 若是不要 `mod.rs`
```rust=
// src/well/magic.rs
pub fn explosion() {
println!("Explosion!")
}
```
```rust=
// src/main.rs
mod well {
pub mod magic;
}
fn main() {
well::magic::explosion();
}
```
---
# crates.io
----
## 簡介
+ Rust 上傳套件的地方
+ 任何人都能在這裡上傳自己的套件
----
## 使用方法
+ 先去 crates.io 註冊帳號
+ 取得 API Token
+ 到自己的專案底下:
1. `cargo login`
2. `cargo publish`
----
## 專案描述
在 `Cargo.toml` 裡面至少要描述 name, version, description, license
```toml=
name = "penut-rs"
version = "0.1.0"
description = "Penut Rust Package"
license = "MIT"
```
----
## 本地端匯入套件
修改 `Cargo.toml` 的 `[dependencies]` 例如:
```toml=
[dependencies]
penut-rs = { path = "/path/to/crate" }
```
---
# clap
----
## 簡介
+ 用來讀取命令列參數的套件
+ 安裝方法:
+ `cargo add clap --features derive`
----
## 用法
```rust=
use clap::Parser;
#[derive(Parser, Debug)]
struct Args {
#[arg(short, long)]
name: String,
#[arg(short, long, default_value_t = 1)]
count: u8,
}
fn main() {
let args = Args::parse();
println!("args: {args:?}");
}
```
----
## 從 cargo 傳入參數
要加上 `--`
```shell=
cargo r -- --name penut
```
---
# Other
----
## Even Better TOML
Logo 看起來就是衝著 CrabLang 來的

[Plugin Link](https://marketplace.visualstudio.com/items?itemName=tamasfe.even-better-toml)
----
## CrabLang
+ [Rust 基金會商標草案遭批評](https://www.ithome.com.tw/news/156386)
+ [Crab Lang](https://github.com/crablang/crab)
{"metaMigratedAt":"2023-06-18T01:19:09.880Z","metaMigratedFrom":"YAML","title":"Week 0E - Crate","breaks":true,"description":"地獄貓旅行團第 14 週心得分享","slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"c7cbb212-2c41-4dfa-8d85-f8e7fa769bf1\",\"add\":3837,\"del\":559}]"}