Search code examples
assemblyrustcompilationcompiler-explorergodbolt

Why the assembly output of a compiled rust code does not include any asm instructions?


I wrote this snippet in the compiler explorer:


fn foo() -> u8 {
    54
}

fn bar() -> u128 {
    3423
}


fn main() {

    let a: (u8, u128) = (foo(), bar());

    println!("foo result: {}", a.0);
    println!("bar result: {}", a.1);

}

The assembly version is this:

        .text
        .file   "example.db4da305-cgu.0"
        .type   __rustc_debug_gdb_scripts_section__,@object
        .section        .debug_gdb_scripts,"aMS",@progbits,1,unique,1
        .weak   __rustc_debug_gdb_scripts_section__
__rustc_debug_gdb_scripts_section__:
        .asciz  "\001gdb_load_rust_pretty_printers.py"
        .size   __rustc_debug_gdb_scripts_section__, 34

        .section        .debug_aranges,"",@progbits
        .section        ".note.GNU-stack","",@progbits

I'm not super familiar with assembly but my little knowledge of it tells me that this doesn't include any asm instructions that implement any of these function. This is only directives.

Am I using compiler explorer in a wrong way? (checking a flag or something)

This is link of my snippet.


Solution

  • You are compiling a library by default. And since your library does not have any public symbol, it does nothing and your asm is empty.

    The solution, either:

    • Declare your library main as public: pub fn main()
    • Compile a binary:

    enter image description here