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

541 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

Changing fn main to pub fn main outputs the correct assembly.

Here is a comment from the rust example:

// Type your code here, or load an example.
pub fn square(num: i32) -> i32 {
    num * num
}

// If you use `main()`, declare it as `pub` to see it in the output: <- This is important
// pub fn main() { ... }