Skip to content

Instantly share code, notes, and snippets.

@rsrini7
Created December 13, 2025 06:12
Show Gist options
  • Select an option

  • Save rsrini7/24b5a5bd7f393770e632e13a90b206e1 to your computer and use it in GitHub Desktop.

Select an option

Save rsrini7/24b5a5bd7f393770e632e13a90b206e1 to your computer and use it in GitHub Desktop.
Rust-vs-C Binary size comparition

Base

1. Initial Compilation & Size Comparison

Timestamp: [00:44] The narrator compiles a basic "Hello World" program in both Rust and C.

  • Commands:
    rustc main.rs -o hello_r
    gcc main.c -o hello_c
    ls -lh  # (Implied command to check file size)
  • Output:
    • hello_r (Rust): ~3.9 MB (described as "almost 4 megabytes").
    • hello_c (C): ~15 KB.

2. Crash Handling Test

Timestamp: [01:01] Both programs are modified to access an array out of bounds to trigger a crash.

  • C Crash:
    • Command: ./hello_c
    • Output: Segmentation fault (or similar OS-level crash message). The narrator notes it "doesn't really tell you where we crashed."
  • Rust Crash:
    • Command: ./hello_r
    • Output:
      thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 10'
      note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
      
    • Follow-up Command: RUST_BACKTRACE=full ./hello_r
    • Output: Displays a detailed stack trace pointing to the exact line of failure.

3. Reducing Binary Size (Stripping Debug Info)

Timestamp: [03:26] The narrator explains that Rust binaries include debug symbols by default, unlike C (in this context).

  • Command:
    rustc -C strip=debuginfo main.rs
  • Output:
    • The binary size drops to ~400 KB (down from ~4 MB).

4. Inspecting Symbols & Linking

Timestamp: [04:25] The narrator uses objdump and ldd to inspect the binaries.

  • Commands:
    ldd hello_c
    ldd hello_r
    objdump -t hello_r  # (Implied flags for symbol table)
  • Output:
    • C: Dynamically links to libc. Has very few symbols.
    • Rust: Statically links the Rust standard library (libstd). Contains many symbols (e.g., mangled names like _ZN4...) because the library code is baked into the executable.

5. Dynamic Linking in Rust

Timestamp: [05:46] To make a "fair" comparison, the narrator compiles Rust to link dynamically against libstd, just like C links against libc.

  • Command:
    rustc -C prefer-dynamic=yes main.rs
  • Output:
    • The binary size drops to ~8 KB (now smaller than the 15 KB C program).

6. Running the Dynamically Linked Rust Binary

Timestamp: [06:51] Trying to run the tiny Rust binary fails because the system cannot find the shared Rust library.

  • Command: ./hello_r
  • Output:
    error while loading shared libraries: libstd-[hash].so: cannot open shared object file: No such file or directory
    

7. Fixing the Path & Final Run

Timestamp: [07:38] To run the dynamically linked binary, the library path must be provided.

  • Command:
    # Path is found in the toolchain directory (e.g., ~/.rustup/toolchains/...)
    export LD_LIBRARY_PATH=/path/to/rust/lib
    ./hello_r
    (Alternatively prepended: LD_LIBRARY_PATH=... ./hello_r)
  • Output:
    • Hello, world! (The program runs successfully).

Source: https://www.youtube.com/watch?v=IXBaXyLdrTA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment