0

C

#include<stdio.h>
#include<string.h>
int main()
{
       char str1[5]="Hello",str2[]=" World!";
       strcat(str1,str2);
       puts(str1);
       return 0;
 }

Size of .s (assembler file) is 1K

Rust

fn main() {
       let str1 = "Hello".to_string();
       let str2 = " World!".to_string();
       println!("{}", str1 + &str2);
}

Size of .s is 107K

Both compiled on Linux x86-64 platform.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Thiyagarajan
  • 39
  • 1
  • 3
  • 1
    Hint: Rust includes a substantial `std` standard library. C generally leans on a dynamically linked `libc`. Check out [`no_std`](https://docs.rust-embedded.org/book/intro/no-std.html), but keep in mind you lose a lot of features you otherwise take for granted. – tadman Nov 05 '22 at 01:29
  • 9
    your C code has a bug, a buffer overflow. – Pablo Nov 05 '22 at 01:31
  • @tadman: `.s` is an assembly-language file, not an executable. – Eric Postpischil Nov 05 '22 at 01:40
  • @EricPostpischil Ah, thought that was a wonky `a.out` choice. Was having trouble understanding the phrasing. – tadman Nov 05 '22 at 01:41
  • 4
    and checking the size of the assembly file is kind of pointless, because there are directives, data, paddings... which are not really instructions – phuclv Nov 05 '22 at 02:33
  • How have you created both `.s` files? What was the exact command in each case? – Cerberus Nov 05 '22 at 03:43
  • 1
    It would only be fair to compare binary sizes, not assembly file sizes, because assembly may include much more than just the code. You also need to have the same linkage and optimization options, as well as have both binaries stripped. To answer your question, compilation commands are important – user15681262 Nov 05 '22 at 06:53
  • 2
    Does this answer your question? [Why are Rust executables so huge?](https://stackoverflow.com/questions/29008127/why-are-rust-executables-so-huge/54842093#54842093) See also: [Removing libstd](https://github.com/johnthagen/min-sized-rust#removing-libstd-with-no_std). – Caesar Nov 05 '22 at 07:46
  • 2
    Incidentally, these two programs don't do the same thing. `String` in Rust is a dynamic array of `u8` which is valid unicode, whereas `char *` in C is just `char *`. – jthulhu Nov 05 '22 at 08:24

0 Answers0