Rust compiler generate a much smaller binary than expected

I have a Rust applications which hardcodes a huge static array (takes more than 30M memory according to my calculation). However, the compiled binary is only about 4M. Does it mean that the Rust compiler compressed the static array into the built binary?

If the compiler can prove the array isn't being used, it can skip including it in the binary. Make sure you're compiling something that actually uses the array.


If you're doing a fairly simple calculation with the array, it may have been constant folded into oblivion.


Also, I'm guessing that this doesn't apply to your case, but if the array were filled with zeros, then it doesn't need to be stored at all — the compiler can just leave a note for the loader "make this symbol point to zeroed memory".


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.