Complete executable sizes, verified on x86-64 Linux:
| Source and build | Bytes | Dependencies |
|---|---|---|
hello-safe.rs: no unsafe code, println! |
286,392 | Shared libc and system libraries |
hello-no-asm.rs: unsafe libc calls, no handwritten assembly |
984 | Shared libc and dynamic loader |
hello.rs: inline syscalls, default linker script, rustc or Cargo |
209 | None |
hello.rs: inline syscalls, Cargo with minimal.ld |
153 | None |
hello.zig: Zig Linux syscall wrappers, default GNU ld script |
213 | None |
hello.zig: Zig Linux syscall wrappers, minimal.ld |
157 | None |
hello-inline.zig: Zig inline syscalls, default GNU ld script |
209 | None |
hello-inline.zig: Zig inline syscalls, minimal.ld |
153 | None |
Shared libraries are excluded from file sizes. Builds use the default linker
script unless minimal.ld is named. All executable files are direct linker
output: no packer or metadata trimming afterward.
With direct rustc builds and no size optimizations, Rust 1.98.1 produces 4,506,168 bytes for hello-safe.rs
and 1,616 bytes for hello.rs. The latter requires -Cpanic=abort,
-Crelocation-model=static, -Clink-arg=-nostartfiles, and -Clink-arg=-static.
These select its panic strategy, fixed-address code, custom entry point, and
direct loading without a dynamic interpreter; they are not default flags.
This unoptimized build uses Rust's default LLD selection; optimized builds use GNU ld.
Rust: rustc 1.98.1 (48a229cea 2026-09-01), toolchain 1.98.1,
target x86_64-unknown-linux-gnu, edition 2024.
Zig: 0.17.0, target x86_64-linux, optimization mode small, LLVM backend.
Compiler downloads: ziglang.org.
Requires Python 3, rustup, GCC, GNU binutils with -z nosectionheader,
and x86-64 Linux. Tested with GNU ld 2.47.20260726; other compiler/linker
versions may produce different sizes.
The Zig builds additionally require Zig 0.17.0.
rustup toolchain install 1.98.1 --profile minimal
python3 build.py
./out/rust-safe
./out/rust-no-asm
./out/rust-cargo-minimal
sh build-zig.sh
./out/hello-minimal
./out/hello-inline-minimalhello-safe.rs uses no unsafe code and uses Rust's standard library.
The other sources use no_std and no_main. hello-no-asm.rs calls libc's puts
and exit; its aligned volatile local makes Rust align the entry-point stack
before those calls. hello.rs uses a #[unsafe(naked)] entry point and
naked_asm! for its complete instruction sequence. Linux supplies zero initial
RAX, RBX, and RDX; mov al, 1, mov dl, 14, and the two xchg instructions
use those values. The compiler emits no prologue, epilogue, or trailing trap.
The entry point must be loaded directly by Linux, with no dynamic interpreter.
hello.zig uses Zig's standard-library Linux syscall wrappers and contains no
handwritten assembly. Its generated code still initializes registers explicitly;
these wrappers do not express the known initial register values or the desired
register exchanges. It remains 157 bytes with minimal.ld.
hello-inline.zig uses callconv(.naked) and the same instruction sequence
as the handwritten assembly and naked Rust, taking 19 bytes. Linker layouts
and message addresses differ; the complete executables are not byte-identical. Both Zig sources use a 14-byte array
to omit the string literal's trailing null byte. The inline versions are
assembly implementations hosted in Rust/Zig, not compiler-generated equivalents.
All versions print Hello, World!\n and exit 0 when writing succeeds.
The naked Rust/Zig versions and the Zig syscall-wrapper version also exit 0
after a returned write error. The two exchanges set the exit number and status
independently of the write result; they no longer assume a successful write.
The safe Rust version may panic on an output error. Syscall versions do not
retry partial writes or suppress signals such as SIGPIPE. Standard and system
libraries contain their own unsafe code.
minimal.ld preserves complete, separate ELF and program headers. No header
bytes contain instructions. GNU ld omits section headers using -z nosectionheader.
The Python script builds, copies Cargo outputs unchanged into out/, and checks
exact output, exit status, extra arguments, and an empty environment for Rust.
It also checks returned ENOSPC and EBADF errors for the naked Rust builds.
build-zig.sh builds Zig with both GNU ld layouts using the same minimal.ld.
Zig outputs were also verified for exact output, exit 0, extra arguments, and
an empty environment. Both Zig versions were checked for exit 0 after
returned write errors.
All tiny syscall variants were checked with a broken pipe and SIGPIPE ignored
so that EPIPE is returned rather than terminating the process.
These are measured results, not proven minima.
ZIG can select a local Zig compiler path.
Based on The Smallest Hello World Program.
Unoptimized reference commands (run from the directory containing the sources):
rustup run 1.98.1 rustc hello-safe.rs --edition=2024 -o hello-safe
rustup run 1.98.1 rustc hello.rs --edition=2024 -Cpanic=abort -Crelocation-model=static -Clink-arg=-nostartfiles -Clink-arg=-static -o helloFor Cargo, save config.toml as .cargo/config.toml, then run cargo build --release.
Add the commented linker-script flag to use minimal.ld. Ordinary cargo build uses the dev profile and does not produce the measured release sizes.