8
votes

Compiling a simple hello world application like this:

fn main() {
    println!("Hello, World!");
}

Generates a relatively huge 822 KB executable using the default compiler options (rustc hello.rs).

Why does this happen and what is the best way to reduce the size of the executable?

2
AFAIK, much or all of the standard library is linked into the binary statically by default. Try optimization -O and/or strip. For kicks and giggles you may also link to libstd etc dynamically (not sure how, though).sellibitze
-Z lto (link-time optimisations) shrinks things plenty too.Chris Morgan

2 Answers

12
votes
  1. The standard library is linked statically by default. You can change that by passing the -C prefer-dynamic option to the compiler.

  2. Rust is still a very young language with an incompletely optimized compiler. There is still a lot of room left for improvements in compilation speed, code speed and size, wording of error messages and so on.

-1
votes

rustc -C opt-level=2 hello.rs gives a 4kb binary