🦀 Rust - Compiling
In Rust, "target" usually refers to the platform you are compiling for. This can be a specific OS, a specific architecture, etc. The target is specified using the --target
flag when running cargo build
or cargo run
.
"Target" can also refer to build target; what are you building.
cargo build --target x86_64-unknown-linux-gnu
# cross-compiling for Windows
rustup target add x86_64-pc-windows-gnu
cargo build --target x86_64-pc-windows-gnu
Platform target defaults to your current host platform.
Platform target has four parts:
<architecture>-<vendor>-<os>-<environment>
x86_64-unknown-linux-gnu
# 64-bit Intel/AMD architecture, unknown vendor, Linux OS, gnu (glibc) ABI
x86_64-apple-darwin
# 64-bit Intel/AMD architecture, Apple vendor, macOS (10.12+, Sierra+)
aarch64-apple-darwin
# 64-bit ARM architecture, Apple vendor, macOS (11.0+, Big Sur+)
wasm32-unknown-unknown
# 32-bit WebAssembly architecture, unknown vendor, unknown OS
Platform target is frequently called "target triple". It actually has four parts but the fourth part is optional.
The --release
flag in Rust is used when building a project to enable optimizations for better performance. It tells cargo
and rustc
to compile the code in release mode instead of the default debug mode.
TL;DR:
Release mode is slower to compile but faster to run.
Debug mode is faster to compile but slower to run.
On Linux, you usually want the GNU ABI. This leads to dynamically linked binaries optimized for modern CPUs with slight runtime overhead and larger size. An alternative is the MUSL ABI that leads to statically linked binaries which are smaller and more secure with leaner memory usage.
# Ubuntu, Debian, etc.
cargo build --target x86_64-unknown-linux-gnu
# Alpine Linux, embedded systems, etc.
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl --release
You shouldn't go for MUSL without a reason, some libraries may not work with it as they expect GNU ABI behavior.