helloworld in rust
Here is a complete, minimal Rust setup that builds self-contained, standalone binaries for Linux, Windows, and macOS.
INSTALLING rust on mac:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer
warn: It looks like you have an existing rustup settings file at:
warn: /Users/amitmund/.rustup/settings.toml
warn: Rustup will install the default toolchain as specified in the settings file,
warn: instead of the one inferred from the default host triple.
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/Users/amitmund/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/Users/amitmund/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/Users/amitmund/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/Users/amitmund/.profile
/Users/amitmund/.bashrc
/Users/amitmund/.zshenv
/Users/amitmund/.tcshrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: aarch64-apple-darwin
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>
info: profile set to default
info: default host triple is aarch64-apple-darwin
info: syncing channel updates for stable-aarch64-apple-darwin
info: latest update on 2026-08-20 for version 1.98.0 (88d9e12ae 2026-08-18)
info: downloading 6 components
cargo installed 8.48 MiB
clippy installed 2.78 MiB
rust-docs installed 23.01 MiB
rust-std installed 28.36 MiB
rustc installed 47.11 MiB
rustfmt installed 1.43 MiB info: default toolchain set to stable-aarch64-apple-darwin
stable-aarch64-apple-darwin installed - rustc 1.98.0 (88d9e12ae 2026-08-18)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.
This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish" # For fish
source "~/.cargo/env.nu" # For nushell
source "$HOME/.cargo/env.tcsh" # For tcsh
. "$HOME/.cargo/env.ps1" # For pwsh
source "$HOME/.cargo/env.xsh" # For xonsh
amitmund@macmini ~ %
1. The Rust Project Setup
Initialize a new binary project:
cargo new sre_worker --bin
cd sre_worker
src/main.rs
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let uuid = args.iter()
.position(|r| r.starts_with("--uuid="))
.map(|i| &args[i][7..])
.unwrap_or("none");
println!("SREToolkit Worker initialized. Bound UUID: {}", uuid);
}
Cargo.toml (Size and Performance Optimizations)
Add release profile optimizations to strip debug symbols and reduce the binary footprint ($< 1.5\text{ MB}$):
[package]
name = "sre_worker"
version = "1.0.0"
edition = "2021"
[profile.release]
opt-level = "z" # Optimize for smallest binary size
lto = true # Link-time optimization across dependencies
codegen-units = 1 # Single codegen unit for maximum optimization
panic = "abort" # Remove stack unwinding machinery
strip = true # Strip symbols and debug info
2. Building for Linux (100% Static via musl)
Standard Linux builds dynamically link to the host system's glibc. Building against musl bundles everything into the binary, allowing it to run on any Linux distribution (Ubuntu, Debian, RHEL, Alpine) without shared library dependencies.
Step 1: Install target and musl tools
# On Ubuntu/Debian host:
sudo apt-get update && sudo apt-get install -y musl-tools
# Add rust target
rustup target add x86_64-unknown-linux-musl
Step 2: Build the binary
cargo build --release --target x86_64-unknown-linux-musl
- Output:
target/x86_64-unknown-linux-musl/release/sre_worker - Verify Static Linkage:
ldd target/x86_64-unknown-linux-musl/release/sre_worker(should return"not a dynamic executable").
3. Building for Windows (.exe with Static C-Runtime)
By default, Rust dynamically links against the Visual C++ runtime DLLs (msvcrt.dll / vcruntime140.dll). You can enforce static CRT linking via .cargo/config.toml.
Step 1: Add Windows Target
rustup target add x86_64-pc-windows-msvc
Step 2: Configure Static CRT
Create .cargo/config.toml in your project root:
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
Step 3: Build the binary (on Windows Host)
cargo build --release --target x86_64-pc-windows-msvc
- Output:
target/x86_64-pc-windows-msvc/release/sre_worker.exe
(Note: If cross-compiling to Windows from Linux, use x86_64-pc-windows-gnu via mingw-w64: sudo apt install mingw-w64 $\rightarrow$ rustup target add x86_64-pc-windows-gnu $\rightarrow$ cargo build --release --target x86_64-pc-windows-gnu).
4. Building for macOS (Universal Mach-O Binary: Intel + Apple Silicon)
macOS does not permit statically linked kernel syscalls (all macOS binaries must dynamically link to libSystem.B.dylib), but this library exists on 100% of macOS installations.
To ensure the binary runs natively on both Intel (x86_64) and Apple Silicon (M1/M2/M3/M4 - aarch64):
Step 1: Add both targets (on macOS Host)
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
Step 2: Compile for both architectures
cargo build --release --target x86_64-apple-darwin
cargo build --release --target aarch64-apple-darwin
Step 3: Combine into a single Universal Binary (lipo)
lipo -create -output sre_worker_darwin_universal \
target/x86_64-apple-darwin/release/sre_worker \
target/aarch64-apple-darwin/release/sre_worker
- Verify Universal Slice:
file sre_worker_darwin_universal(outputs:Mach-O universal binary with 2 architectures: [x86_64] [arm64]).
5. Cross-Platform Automation via cross (Optional)
If you want to build Linux, Windows, and macOS artifacts all from a single Linux development machine, use the cross tool, which runs the toolchain inside isolated pre-configured Docker containers:
cargo install cross --git https://github.com/cross-rs/cross
# Build static Linux musl from anywhere:
cross build --release --target x86_64-unknown-linux-musl
# Build static Windows exe from Linux:
cross build --release --target x86_64-pc-windows-gnu
You can build static Linux binaries directly from a Mac in Rust.
The only difference between Go and Rust comes down to the linker:
- Go includes its own built-in pure Go linker, so
CGO_ENABLED=0 GOOS=linux go buildworks with zero extra setup. - Rust uses LLVM for cross-compilation (built-in), but still requires an external linker targeted for Linux
musl.
The Easiest Way: cargo-zigbuild (No Docker Required)
cargo-zigbuild uses zig as a drop-in C/C++ cross-compiler and linker. It lets you compile pure static musl Linux binaries directly on macOS with two commands.
Step 1: Install Zig and cargo-zigbuild on macOS
brew install zig
cargo install cargo-zigbuild
Step 2: Add the Linux musl target
# For x86_64 Linux (Intel/AMD)
rustup target add x86_64-unknown-linux-musl
# Or for ARM64 Linux (AWS Graviton / Raspberry Pi)
rustup target add aarch64-unknown-linux-musl
Step 3: Build the static Linux binary
cargo zigbuild --release --target x86_64-unknown-linux-musl
- Output:
target/x86_64-unknown-linux-musl/release/sre_worker - This produces a 100% statically linked ELF binary that runs on any Linux distribution without external dependencies.
Alternative: Using cross (Docker-based)
If you already have Docker or Podman running on your Mac, cross automates the entire process in a container:
cargo install cross
cross build --release --target x86_64-unknown-linux-musl
cross is the gold standard for cross-compiling Rust when you want an automated, zero-configuration setup.
How cross Works
cross provides the exact same CLI as cargo, but executes the compilation inside pre-configured OCI/Docker container images containing the cross-toolchain, sysroots, and target linkers.
Host Machine (Mac / Linux / Windows)
│
├── Runs: cross build --target <TRIPLET>
│
└── Launches Docker/Podman Container
├── Correct Target Linker (musl-gcc, mingw, etc.)
├── Correct C Headers & Libs
└── Emits binary into your local target/ directory
1. Prerequisites
You need Docker (or Podman/OrbStack) running on your machine.
Install cross:
cargo install cross --git https://github.com/cross-rs/cross
(Or via cargo-binstall cross / direct GitHub binary download).
2. Compiling All Platforms with cross
You can run these commands from your Mac (Intel or Apple Silicon):
A. Linux Static Binary (musl — AMD64/x86_64)
cross build --release --target x86_64-unknown-linux-musl
- Output:
target/x86_64-unknown-linux-musl/release/sre_worker - 100% standalone, no glibc dependency.
B. Linux Static Binary (ARM64 — Graviton / Raspberry Pi)
cross build --release --target aarch64-unknown-linux-musl
- Output:
target/aarch64-unknown-linux-musl/release/sre_worker
C. Windows Static .exe (GNU / MinGW)
cross build --release --target x86_64-pc-windows-gnu
- Output:
target/x86_64-pc-windows-gnu/release/sre_worker.exe - Self-contained Windows executable compiled without needing a Windows machine.
D. macOS Target
- If on macOS Host: Build natively with standard cargo:
cargo build --release --target x86_64-apple-darwin
cargo build --release --target aarch64-apple-darwin
lipo -create -output sre_worker_darwin_universal \
target/x86_64-apple-darwin/release/sre_worker \
target/aarch64-apple-darwin/release/sre_worker
- (Note: Apple licensing restricts redistributing macOS SDKs inside public Docker containers, so building macOS targets via
crosson non-Mac hosts requires a customosxcrossimage. Building macOS binaries directly on your Mac host is natively supported).
3. Optional: Cross.toml Customization
If your Rust code ever depends on C libraries (like OpenSSL or SQLite), you can configure Cross.toml in your project root to control the container environment:
# Cross.toml
[target.x86_64-unknown-linux-musl]
# Pass custom build environment variables into the container
env = { PASSTHROUGH = ["RUST_LOG"] }
Summary Comparison
| Method | Docker Required? | Best For |
|---|---|---|
cross |
Yes | Zero setup across complex C-dependency crates and embedded targets. |
cargo-zigbuild |
No | Lightweight, instant static Linux/Windows compilation on macOS without running a container engine. |
Automated Multi-Platform Cross-Compilation Helper (build_workers.sh)
#!/usr/bin/env bash
set -euo pipefail
DIST_DIR="dist/bin"
mkdir -p "$DIST_DIR"
echo "=== Building SREToolkit Static Workers ==="
# 1. Linux Static (x86_64 Musl) via cross
echo "[+] Compiling Linux x86_64 static (musl)..."
cross build --release --target x86_64-unknown-linux-musl
cp target/x86_64-unknown-linux-musl/release/sre_worker "$DIST_DIR/sre_worker_linux_amd64"
# 2. Windows Static (x86_64 GNU) via cross
echo "[+] Compiling Windows x86_64 static (.exe)..."
cross build --release --target x86_64-pc-windows-gnu
cp target/x86_64-pc-windows-gnu/release/sre_worker.exe "$DIST_DIR/sre_worker_windows_x64.exe"
# 3. macOS Universal Binary (Intel + Apple Silicon)
if [[ "$(uname)" == "Darwin" ]]; then
echo "[+] Compiling macOS Universal Binary..."
cargo build --release --target x86_64-apple-darwin
cargo build --release --target aarch64-apple-darwin
lipo -create -output "$DIST_DIR/sre_worker_darwin_universal" \
target/x86_64-apple-darwin/release/sre_worker \
target/aarch64-apple-darwin/release/sre_worker
fi
echo "=== Computing SHA-256 Checksums for commands.toml ==="
for file in "$DIST_DIR"/*; do
if [ -f "$file" ]; then
if command -v sha256sum &>/dev/null; then
sha256sum "$file"
else
shasum -a 256 "$file"
fi
fi
done