Release Build & Cross-Compilation
ปัญหา deployment ของ Node.js
หัวข้อที่มีชื่อว่า “ปัญหา deployment ของ Node.js”เมื่อ 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 / Node.js deployment# ต้องส่ง runtime + packages ทั้งหมดdist/ index.js # app ที่ bundle แล้วnode_modules/ # 300+ MB of dependencies# ... plus ต้องติดตั้ง Node บน server
# หรือใช้ Docker:FROM node:24-alpineCOPY package*.json ./RUN npm ci --only=productionCOPY dist/ ./dist/CMD ["node", "dist/index.js"]# Rust deployment# Build ครั้งเดียว ส่ง binarycargo build --release# → target/release/my-app (single static binary, ~5 MB)
# Copy binary ไปที่ server — แค่นั้น# ไม่มี runtime, ไม่มี package manager, ไม่มี node_modulesscp target/release/my-app user@server:/usr/local/bin/Debug vs release build
หัวข้อที่มีชื่อว่า “Debug vs release build”cargo build # debug build: fast compile, unoptimised, includes debug infocargo build --release # release build: slow compile, fully optimised (LLVM -O3)cargo run --release # compile in release mode and runDebug build ใช้ระหว่าง development — compile ใน second Release build อาจใช้เวลาหลายนาทีสำหรับ project ใหญ่แต่สร้าง binary ที่เร็วกว่าที่ runtime บ่อยถึง 10–100 เท่า
Cross-compilation
หัวข้อที่มีชื่อว่า “Cross-compilation”Rust รองรับ cross-compilation โดยตรง target triple ระบุ OS + architecture + ABI combination
# Add a cross-compilation targetrustup 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 targetcargo build --release --target x86_64-unknown-linux-musl# → target/x86_64-unknown-linux-musl/release/my-appStatic binary ด้วย musl
หัวข้อที่มีชื่อว่า “Static binary ด้วย musl”target x86_64-unknown-linux-musl link กับ musl libc แทน glibc สร้าง static binary เต็มรูปแบบที่รันบน Linux distribution ใดก็ได้ — รวมถึง Alpine Linux container — โดยไม่มี shared library dependency
# One-time setuprustup target add x86_64-unknown-linux-musl
# On macOS you need a cross-linker; easiest via cargo-zigbuild:cargo install cargo-zigbuildcargo zigbuild --release --target x86_64-unknown-linux-musl
# Verify: the binary has no dynamic dependenciesldd target/x86_64-unknown-linux-musl/release/my-app# → not a dynamic executableOptimisation profile ใน Cargo.toml
หัวข้อที่มีชื่อว่า “Optimisation profile ใน Cargo.toml”[profile.release]opt-level = 3 # maximum LLVM optimisation (default)lto = true # link-time optimisation: smaller + faster binarycodegen-units = 1 # single codegen unit: slower compile, better optimisationstrip = true # strip debug symbols from the binarypanic = "abort" # abort on panic instead of unwinding (smaller binary)เหล่านี้คือ
bash/tomlconfiguration ไม่มี browser Playground สำหรับ cross-compilation — รัน command เหล่านี้ใน terminal หลังติดตั้ง Rust ผ่าน rustup.rs