How to optimize the size of the executable binary file for native and WASM toolchains?

Viewed 858

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?

1 Answers

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.

Related