Why does rust produce at least 1G of target files after each compilation? What we only need is the binary execution file inside. Is it necessary to generate other files?

Does cargo have any functions, or just generate binary files when executing cargo run. Do not generate other files? Because after each cargo clean, the next compilation will generate a lot of files that do not know what the purpose is, which really occupies a lot of memory. For beginners with low computer configuration, learning rust is really too persuasive.

1 Like

These files are caches that make incremental compilation faster. It's ideal to have a large disk and fit everything, but for many that's not available. Several ways to reduce your disk pressure:

  • sccache, which lets you deduplicate some files across projects. Not much downsides, other than more complicated setup.
  • cargo clean, if you're not going to touch the project for a long time. This will cause Rust to regenerate all the files upon the next compilation, making it slower.
  • Going over your dependencies and removing the ones you don't need. This is free for some crates, but removing too much might make you reinvent things.

Less related to Rust but as a general advice, your computer, especially if Windows, might contain some bloatwares you could uninstall. That include anything you haven't used with a company's name in front of it, even those that call itself "file cleaner" or "antivirus"(excepting those that are part of Windows). This can open up few more gigabytes in addition to making the computer run faster.

The "why is it so big" question is actually pretty good, though: for whatever reason, modern native code generation involves a tremendous amount of intermediate files, orders of magnitude larger than the input sources and final output. Tracking additional information for incremental builds, line numbers, type info and so on all will necessarily increase the size, but the magnitude of the increase does seem outsized, and I'd love to know why.


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.