title: Why does Rust emit 280 lines of assembly code to print "Hello, world"?
body: <p>A simple 3-line Hello, World program in Rust emits a 280 line assembly file in order to print two words. Meanwhile, <a href="https://gist.github.com/FiloSottile/7125822" rel="nofollow noreferrer">a pure assembly version of this program</a> only takes ~15 lines. Why is the Rust version so much longer?</p>
  
  <p>This is on Mac OS.</p>
  
  <p><strong><code>hello.rs</code></strong>:</p>
  
  <pre><code>fn main() {
      println!("hello world")
  }
  </code></pre>
  
  <p><strong><code>hello.s</code></strong> (generated by <code>rustc --emit=asm hello.rs</code>):</p>
  
  <pre><code>    .section    __TEXT,__text,regular,pure_instructions
      .macosx_version_min 10, 7
      .private_extern __ZN3std2rt10lang_start17hb4e01c1e588bf694E
      .globl  __ZN3std2rt10lang_start17hb4e01c1e588bf694E
      .p2align    4, 0x90
  __ZN3std2rt10lang_start17hb4e01c1e588bf694E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $32, %rsp
      leaq    l___unnamed_1(%rip), %rax
      movq    %rdi, -8(%rbp)
      leaq    -8(%rbp), %rcx
      movq    %rcx, %rdi
      movq    %rsi, -16(%rbp)
      movq    %rax, %rsi
      movq    -16(%rbp), %rax
      movq    %rdx, -24(%rbp)
      movq    %rax, %rdx
      movq    -24(%rbp), %rcx
      callq   __ZN3std2rt19lang_start_internal17hcf96e32a124891dcE
      movq    %rax, -32(%rbp)
      movq    -32(%rbp), %rax
      addq    $32, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h275cd8632ff3ab7dE:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      callq   *(%rdi)
      callq   __ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17hf5e270b394827df3E
      movl    %eax, -4(%rbp)
      movl    -4(%rbp), %eax
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN3std3sys4unix7process14process_common8ExitCode6as_i3217h7e671b2505e0c229E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      movzbl  (%rdi), %eax
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN4core3fmt9Arguments6new_v117h39ef65f250941772E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      movq    %rdi, %rax
      movq    $0, -16(%rbp)
      movq    %rsi, (%rdi)
      movq    %rdx, 8(%rdi)
      movq    -16(%rbp), %rdx
      movq    -8(%rbp), %rsi
      movq    %rdx, 16(%rdi)
      movq    %rsi, 24(%rdi)
      movq    %rcx, 32(%rdi)
      movq    %r8, 40(%rdi)
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h61f8ee8d3fead017E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      movq    (%rdi), %rdi
      callq   __ZN4core3ops8function6FnOnce9call_once17h47f538be1b10688dE
      movl    %eax, -12(%rbp)
      movl    -12(%rbp), %eax
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN4core3ops8function6FnOnce9call_once17h47f538be1b10688dE:
  Lfunc_begin0:
      .cfi_startproc
      .cfi_personality 155, _rust_eh_personality
      .cfi_lsda 16, Lexception0
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $48, %rsp
      movq    %rdi, -32(%rbp)
  Ltmp0:
      leaq    -32(%rbp), %rdi
      callq   __ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h275cd8632ff3ab7dE
  Ltmp1:
      movl    %eax, -36(%rbp)
      jmp LBB5_1
  LBB5_1:
      jmp LBB5_2
  LBB5_2:
      movl    -36(%rbp), %eax
      addq    $48, %rsp
      popq    %rbp
      retq
  LBB5_3:
      jmp LBB5_4
  LBB5_4:
      movq    -16(%rbp), %rdi
      callq   __Unwind_Resume
      ud2
  LBB5_5:
  Ltmp2:
      movq    %rax, -16(%rbp)
      movl    %edx, -8(%rbp)
      jmp LBB5_3
  Lfunc_end0:
      .cfi_endproc
      .section    __TEXT,__gcc_except_tab
      .p2align    2
  GCC_except_table5:
  Lexception0:
      .byte   255
      .byte   255
      .byte   1
      .uleb128 Lcst_end0-Lcst_begin0
  Lcst_begin0:
      .uleb128 Ltmp0-Lfunc_begin0
      .uleb128 Ltmp1-Ltmp0
      .uleb128 Ltmp2-Lfunc_begin0
      .byte   0
      .uleb128 Ltmp1-Lfunc_begin0
      .uleb128 Lfunc_end0-Ltmp1
      .byte   0
      .byte   0
  Lcst_end0:
      .p2align    2
  
      .section    __TEXT,__text,regular,pure_instructions
      .p2align    4, 0x90
  __ZN4core3ptr18real_drop_in_place17h0ab16307507408dbE:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN54_$LT$$LP$$RP$$u20$as$u20$std..process..Termination$GT$6report17hf5e270b394827df3E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      xorl    %edi, %edi
      callq   __ZN68_$LT$std..process..ExitCode$u20$as$u20$std..process..Termination$GT$6report17h03f2ed18f1614f97E
      movl    %eax, -4(%rbp)
      movl    -4(%rbp), %eax
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN68_$LT$std..process..ExitCode$u20$as$u20$std..process..Termination$GT$6report17h03f2ed18f1614f97E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      movb    %dil, -1(%rbp)
      leaq    -1(%rbp), %rdi
      callq   __ZN3std3sys4unix7process14process_common8ExitCode6as_i3217h7e671b2505e0c229E
      movl    %eax, -8(%rbp)
      movl    -8(%rbp), %eax
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .p2align    4, 0x90
  __ZN5hello4main17hef70db39c48df377E:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $48, %rsp
      leaq    l___unnamed_2(%rip), %rax
      xorl    %ecx, %ecx
      movl    %ecx, %r8d
      leaq    -48(%rbp), %rdi
      movq    %rax, %rsi
      movl    $1, %edx
      movl    $8, %ecx
      callq   __ZN4core3fmt9Arguments6new_v117h39ef65f250941772E
      leaq    -48(%rbp), %rdi
      callq   __ZN3std2io5stdio6_print17hd8f597a6d310dad5E
      addq    $48, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .globl  _main
      .p2align    4, 0x90
  _main:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset %rbp, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register %rbp
      subq    $16, %rsp
      movslq  %edi, %rax
      leaq    __ZN5hello4main17hef70db39c48df377E(%rip), %rdi
      movq    %rsi, -8(%rbp)
      movq    %rax, %rsi
      movq    -8(%rbp), %rdx
      callq   __ZN3std2rt10lang_start17hb4e01c1e588bf694E
      addq    $16, %rsp
      popq    %rbp
      retq
      .cfi_endproc
  
      .section    __DATA,__const
      .p2align    3
  l___unnamed_1:
      .quad   __ZN4core3ptr18real_drop_in_place17h0ab16307507408dbE
      .quad   8
      .quad   8
      .quad   __ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h275cd8632ff3ab7dE
      .quad   __ZN3std2rt10lang_start28_$u7b$$u7b$closure$u7d$$u7d$17h275cd8632ff3ab7dE
      .quad   __ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h61f8ee8d3fead017E
  
      .section    __TEXT,__const
  l___unnamed_3:
      .ascii  "hello world\n"
  
      .section    __DATA,__const
      .p2align    3
  l___unnamed_2:
      .quad   l___unnamed_3
      .asciz  "\f\000\000\000\000\000\000"
  
  
  .subsections_via_symbols
  </code></pre>