Why do rust binaries are so huge

i'm doing a dd like program, and with the release mode on and the strip and panic=abort the binary keep huge

400kb compared to dd native: 62kb

There's a repo with tips and tricks for reducing binary size of Rust programs:

2 Likes

Another reason is that C binaries tend to dynamically link to libraries,[1] whereas your Rust programs don't.


  1. e.g. all supplied by your distro ↩ī¸Ž

One contributing factor is that the rust standard library is statically linked to the application binary. There's no equivalent to libc.so for Rust in your operating system that all your programs can link against.

With that said, Rust does does come with a dynamic standard library. If you use it, your binary will be substantially smaller -- but the binary will be Rust toolchain version specific.

You can try building your program using RUSTFLAGS="-C prefer-dynamic" cargo build to see the difference.

Outside of very specific use-cases, I don't think it's particularly useful to dynamically link std -- but it is a piece of the puzzle in understand why Rust binaries are so big.

1 Like

Also a big contributing factor is the fact that we are shipping an backtrace symbolizer for the implementation of RUST_BACKTRACE=1. We now do have a CI job tracking the binary size overhead for this in GitHub - rust-lang/backtrace-rs: Backtraces in Rust to avoid regressions and to make it easier to track improvements.

3 Likes

That particular difference is pure difference in linking:

$ cat hello.c
#include <stdio.h>

int main() {
  printf("Hellow, world!");
}
$ gcc -O3 -static hello.c -o hello
$ ls -al a.out
-rwxr-x--- 1 khim khim 740472 Jan 22 13:17 a.out
1 Like