2
fn main() {
    println!("hello");
}

This program compiles 600 ms and the resulting binary is 600KB in size. Why is that? I am just trying Rust, and comparing it to C. C would compile similar program 10 times faster and the binary output will be 100 times smaller. So why is that?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
exebook
  • 32,014
  • 33
  • 141
  • 226
  • To half-answer the other half of the question: Rust takes longer to compile than C because the source code you're compiling can contain more information. It needs to have more information because that's how it can make stronger guarantees about memory safety. And that applies even in exceedingly simple programs because rustc can't know how complex your code is before it compiles it, so all that machinery is in place and running regardless. – Nic Oct 30 '22 at 20:08

1 Answers1

13

The executable size is mostly because rust's standard library is statically linked in by default. Try compiling with rustc -O -C prefer-dynamic and you should get a binary that's comparable to the C version.

fjh
  • 12,121
  • 4
  • 46
  • 46