Rust Compiled binary Size- Dynamic Linking

When I compile a Hello World program in Rust, the binary size is huge. It is because,
it is monolithic and every thing is statically linked.

How to do dynamic linking for example in Linux platform ?.


You may find Minimizing Rust Binary Size useful.

Rust statically links its own standard lib and your project dependencies, but if your dependencies have C deps through the FFI, they will be dynamically linked. You can use ldd on your executable to check.

If you really need full static linking, the musl target can help with this.

I think @sgopinath wants dynamic linking, not static linking.

Just checking but have you tried cargo build --release?

Other than that, optimizing for "hello world" is not often very useful.

1 Like

Thanks everyone who helps me about dynamic linking. I suppose it would be nice if we could compile Rust standard libraries and other packages and install as shareable and could be dynamically link.

If such mechanism already exists may please indicate.

In some cases, the size of the binary goes to very large size.


A hello world program is not really representative for how the size of the binary grows as the program becomes more complex.

In particular there are items from stdlib that are pretty much always included, and these represent a constant overhead no matter the complexity of the program itself.

As a side note: trying to optimize the size of a hello world program is very very likely to be premature optimization.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.