DEV Community

Discussion on: First Week learning rust

Collapse
 
lexiebkm profile image
Alexander B.K.

I have build a very simple, console hello world program using Rust, Go and C++ on Windows. The executable file size is surprising to me :
Rust is 4Mb, Go is about 3Mb, while C++ is only 171kb (as expected). For C++ I use g++ compiler. I installed Rust using its GNU version of Msi installer.
Can you explain why ?
When I have developed a relatively large web app using PHP+Laravel in backend as well as React, Bootstrap and other 3rd party libs in frontend that has a lot of forms, the bundle size was about 8Mb which I thought was very big. I couldn't use code-splitting at that time.
Now, seeing the 3-4Mb for a console hello world that basically only has one printLn using both Rust and Go seems to be unacceptable.

Collapse
 
deciduously profile image
Ben Lovy

I don't know about Go, so I can't speak for that. With Rust, you're seeing these sizes due to static linking, and symbols being included in the binary. On Linux, a hello-world compiled with --release generates a 3.7M executable. Just running strip on this executable reduces the size to 310k. You can also se size-related optimizations in your profile:

[profile.release]
opt-level = "z"
lto = true
Enter fullscreen mode Exit fullscreen mode

Other tweakables include panic behavior - you can remove the unwinding mechanism using this:

[profile.release]
panic = "abort"
Enter fullscreen mode Exit fullscreen mode

So, yes, while the defaults produce some hefty executables, Rust does expose tools to more finely tweak your result. This repo provides a much more comprehensive overview.

Thread Thread
 
lexiebkm profile image
Alexander B.K.

Thanks for the answer. This is just a start for my journey of Rust. I have a plan to try it for web backend after reading your post about it. We know that the book also has a chapter on web server that I can use for starting point.

Collapse
 
peerreynders profile image
peerreynders • Edited

The executable file size is surprising to me : Rust is 4Mb …

Quote

The executable is 3.5 MiB but there's only 212 KiB of actual executable code, the rest is debug symbols.

Minimizing Rust Binary Size

With C/C++ knowledge Programming Rust 2e may be a better starting point — though if that knowledge is spotty, it could still become a hard slog.


TinyGo - Go compiler for small places


FYI: Zig

Thread Thread
 
lexiebkm profile image
Alexander B.K.

Thanks for the explanation, sir.