Why are Rust executables so huge?

Rust uses static linking to compile its programs, meaning that all libraries required by even the simplest Hello world! program will be compiled into your executable. This also includes the Rust runtime.

To force Rust to dynamically link programs, use the command-line arguments -C prefer-dynamic; this will result in a much smaller file size but will also require the Rust libraries (including its runtime) to be available to your program at runtime.
This essentially means you will need to provide them if the computer does not have them, taking up more space than your original statically linked program takes up.

For portability I’d recommend you statically link the Rust libraries and runtime in the way you have been doing if you were to ever distribute your programs to others.

Leave a Comment