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

Release Build & Cross-Compilation

เมื่อ deploy Node.js application คุณต้องส่ง Node เองไปด้วย — runtime, node_modules และ dependency ทั้งหมด ผลที่ได้มักมีขนาดหลายร้อย MB การ deploy มักหมายความว่าต้อง bundle ด้วย esbuild/webpack, เขียน Dockerfile ที่ติดตั้ง Node หรือใช้ PaaS ที่รู้จักการรัน Node

Rust compile เป็น binary ที่ self-contained เดียว ไม่มี runtime ที่ต้องส่ง, ไม่มี node_modules และไม่มี interpreter binary รันบน OS โดยตรง

TypeScript
# TypeScript / Node.js deployment
# ต้องส่ง runtime + packages ทั้งหมด
dist/
index.js # app ที่ bundle แล้ว
node_modules/ # 300+ MB of dependencies
# ... plus ต้องติดตั้ง Node บน server
# หรือใช้ Docker:
FROM node:24-alpine
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/
CMD ["node", "dist/index.js"]
Rust
# Rust deployment
# Build ครั้งเดียว ส่ง binary
cargo build --release
# → target/release/my-app (single static binary, ~5 MB)
# Copy binary ไปที่ server — แค่นั้น
# ไม่มี runtime, ไม่มี package manager, ไม่มี node_modules
scp target/release/my-app user@server:/usr/local/bin/
Terminal window
cargo build # debug build: fast compile, unoptimised, includes debug info
cargo build --release # release build: slow compile, fully optimised (LLVM -O3)
cargo run --release # compile in release mode and run

Debug build ใช้ระหว่าง development — compile ใน second Release build อาจใช้เวลาหลายนาทีสำหรับ project ใหญ่แต่สร้าง binary ที่เร็วกว่าที่ runtime บ่อยถึง 10–100 เท่า

Rust รองรับ cross-compilation โดยตรง target triple ระบุ OS + architecture + ABI combination

Terminal window
# Add a cross-compilation target
rustup target add x86_64-unknown-linux-musl # Linux, statically linked (musl libc)
rustup target add aarch64-unknown-linux-gnu # Linux, ARM64 (Raspberry Pi, AWS Graviton)
rustup target add x86_64-pc-windows-gnu # Windows from macOS/Linux
# Compile for a different target
cargo build --release --target x86_64-unknown-linux-musl
# → target/x86_64-unknown-linux-musl/release/my-app

target x86_64-unknown-linux-musl link กับ musl libc แทน glibc สร้าง static binary เต็มรูปแบบที่รันบน Linux distribution ใดก็ได้ — รวมถึง Alpine Linux container — โดยไม่มี shared library dependency

Terminal window
# One-time setup
rustup target add x86_64-unknown-linux-musl
# On macOS you need a cross-linker; easiest via cargo-zigbuild:
cargo install cargo-zigbuild
cargo zigbuild --release --target x86_64-unknown-linux-musl
# Verify: the binary has no dynamic dependencies
ldd target/x86_64-unknown-linux-musl/release/my-app
# → not a dynamic executable
[profile.release]
opt-level = 3 # maximum LLVM optimisation (default)
lto = true # link-time optimisation: smaller + faster binary
codegen-units = 1 # single codegen unit: slower compile, better optimisation
strip = true # strip debug symbols from the binary
panic = "abort" # abort on panic instead of unwinding (smaller binary)

เหล่านี้คือ bash/toml configuration ไม่มี browser Playground สำหรับ cross-compilation — รัน command เหล่านี้ใน terminal หลังติดตั้ง Rust ผ่าน rustup.rs

Command ใดสร้าง Rust binary ที่ optimise สำหรับ production?
ข้อได้เปรียบหลักของ target `x86_64-unknown-linux-musl` คืออะไร?
Setting `[profile.release]` ใดตัด debug symbol ออกจาก binary?