mike,
@mike@fosstodon.org avatar

I installed rustc tonight and proceeded to write HelloWorld.rs. My first ever :rust: program. And it's 13MB. What the hell??

thomy2000,
@thomy2000@fosstodon.org avatar

@mike https://github.com/johnthagen/min-sized-rust

If you like small binaries :blobcatMelt3:

underlap,
@underlap@fosstodon.org avatar

@mike I reproduced your hello world code (using cargo new hello --bin).

cargo build produces a debug executable of 3.8 MB on Linux.

But cargo build --release produces a 420 KB executable without debug symbols etc.

(I believe it's possible to shrink the executable right down to a minimal form that depends on libc but I couldn't get that to work easily.)

mike,
@mike@fosstodon.org avatar

@underlap I'm going to have to spend more time on this. I tried cargo build --release, and it complained that I didn't have a Cargo.toml file, so I created a basic one that looks like this:

[package]
name = "hello_world"
version = "0.0.1"

[[bin]]
name = "HelloWorld"
path = "hello_world.rs"

The build created a directory called ./target with a release directory under it, which contained the binary and several other files. It was still 13MB.

Obviously I don't know what I'm doing. At all.

orsinium,
@orsinium@fosstodon.org avatar

@mike @underlap

Strange. What's your OS? And regardless of the answer, maybe you can try building in a Linux Docker image and comparing? Let's see if a clean environment gives the right result.

It should give for Linux under 4 Mb for debug builds and under 600 Kb fir release ones. And then there are ways to squeeze more when it matters:

https://github.com/johnthagen/min-sized-rust

I have a real fat Rust project running on PyBadge that is under 300 Kb binary.

underlap,
@underlap@fosstodon.org avatar

@mike That's odd. If you want to chase the problem, create another project using cargo new --bin, check that produces a much smaller release binary, then play spot the difference with your project.

Also cargo clean may help sort out your project.

Bene,
@Bene@fosstodon.org avatar

@mike it's about the same in golang, but hello world is around 2,2mb only.

urusan,
@urusan@fosstodon.org avatar

@mike My favorite language, Julia, has a similar issue. It's statically compiling everything it needs together by default to ensure portability. In particular the Rust runtime is pretty substantial (and the case of Julia is even worse since it includes all of LLVM).

You can tell Rust to knock it off and do dynamic linking, but then the target machine will need the Rust runtime installed.

There are other options as well, but I'm not a Rust expert so I don't know the details.

JasonPunyon,
@JasonPunyon@fosstodon.org avatar
  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • tester
  • magazineikmin
  • khanakhh
  • InstantRegret
  • thenastyranch
  • Youngstown
  • everett
  • mdbf
  • slotface
  • ngwrru68w68
  • DreamBathrooms
  • kavyap
  • osvaldo12
  • rosin
  • JUstTest
  • Durango
  • tacticalgear
  • modclub
  • cubers
  • GTA5RPClips
  • ethstaker
  • normalnudes
  • cisconetworking
  • Leos
  • megavids
  • provamag3
  • anitta
  • lostlight
  • All magazines