ข้ามไปยังเนื้อหา

Modules and Crates

TypeScript ใช้ ESM import/export สำหรับการจัดระเบียบโค้ด และใช้ npm (หรือคล้ายกัน) สำหรับแพ็กเกจจากภายนอก ส่วน Rust มีสองแนวคิดที่ขนานกัน: modules (mod/use) สำหรับจัดระเบียบโค้ดภายในโปรเจกต์ และ crates (แพ็กเกจที่จัดการโดย Cargo) สำหรับ dependency จากภายนอก

module จัดกลุ่ม item ที่เกี่ยวข้องกันไว้ด้วยกัน ใช้ pub เพื่อทำให้ item มองเห็นได้จากภายนอก module — ค่าเริ่มต้นคือ private

// src/math.rs (a module file)
pub fn add(a: i32, b: i32) -> i32 { a + b }
fn helper() { } // private — not accessible outside
// src/main.rs
mod math; // declare the module (pulls in math.rs)
use math::add; // bring add into scope
fn main() {
println!("{}", add(1, 2));
}

module ยังประกาศแบบ inline ได้ด้วย mod name { ... }

crate คือตัวเทียบเท่ากับแพ็กเกจ npm ของ Rust ส่วน Cargo.toml ก็คือ package.json workflow แมปกันได้โดยตรง:

npmCargo
npm install serdecargo add serde
package.jsonCargo.toml
node_modules/~/.cargo/registry/ (cache ที่ใช้ร่วมกัน)
import { x } from 'pkg'use pkg::x;
  • item เป็น private โดยปริยาย — เข้าถึงได้เฉพาะใน module เดียวกันและ module ลูกหลานเท่านั้น
  • pub ทำให้ item เข้าถึงได้จากทุกที่
  • pub(crate) จำกัด visibility ให้อยู่แค่ภายใน crate ปัจจุบันเท่านั้น
  • pub(super) จำกัดให้อยู่แค่ module แม่
TypeScript
// math.ts — named exports
export function add(a: number, b: number): number {
return a + b;
}
function helper() {} // not exported = module-private
// main.ts
import { add } from './math';
console.log(add(1, 2));
// package.json dependency
// "serde": "1.0" (hypothetical)
Rust
// src/math.rs
pub fn add(a: i32, b: i32) -> i32 { a + b }
fn helper() {} // private
// src/main.rs
mod math;
use math::add;
fn main() {
println!("{}", add(1, 2));
}
// Cargo.toml dependency
// [dependencies]
// serde = "1"

หมายเหตุ Playground: โครงสร้าง module หลายไฟล์ต้องใช้ cargo — รันบนเครื่องของคุณด้วย cargo run

visibility โดยปริยายของ function ใน module ของ Rust คืออะไร?
อะไรคือตัวเทียบเท่าของ `npm install serde` ใน Rust?
`use std::collections::HashMap;` ทำอะไรใน Rust?