4

I am new to Rust and so far I was amazed by its design. But I encountered something that makes me be scared to use it in commercial projects. The size of the executable binary file of a "Hello world" application is 3.2Mb.

-rwxr-xr-x 2 kos kos 3,2M Jul 10 15:44 experiment_app_size

That's huge!

  • The version of rustc is 1.53.0
  • The toolchain is stable-x86_64-unknown-linux-gnu.
  • Target is release.

I am wondering is it planned to fix the problem in the future? Is there a technique I can use to decrease the size of the executable binary file? Is the same problem relevant to WASM toolchain?

Kos
  • 1,547
  • 14
  • 23
  • 3
    This has been frequently discussed, on this site and elsewhere. Start with [Why are Rust executables so huge?](https://stackoverflow.com/questions/29008127/why-are-rust-executables-so-huge) and try some basic web searches like "rust binary size". – IMSoP Jul 10 '21 at 13:19
  • @IMSoP 6 years old. Is that still relevant? – Kos Jul 10 '21 at 13:32
  • I have no idea, but your question as written shows no evidence of even trying to find existing information. – IMSoP Jul 10 '21 at 13:39
  • @IMSoP It's indeed outdated and has no answer regarding the WASM toolchain. There is not much information about size optimization in official documentation, but the article I shared was recommended to me is good. I need to find an answer regarding the WASM toolchain. The post you refer to does not cover the WASM. – Kos Jul 10 '21 at 14:14
  • 1
    In that case, you should either: add an answer to the existing question; or ask a question focussing specifically on the WASM toolchain. As currently worded, this question is asking exactly the same thing as the existing one. – IMSoP Jul 10 '21 at 14:17

1 Answers1

6

By default, Rust optimizes for execution speed rather than binary size, since for the vast majority of applications this is ideal. But for situations where a developer wants to optimize for binary size instead, Rust provides mechanisms to accomplish this.

Most techniques described above are applicable to both native and WASM toolchains. Following that guide, it is possible to get a "hello world" binary around 93k.

And here is a specialized extensive article on how to optimize the size of binary of Rust WASM build.

And here is a deep discussion on the official Rust forum of the pros and cons of options developer has to optimize binary by size.

Kos
  • 1,547
  • 14
  • 23