Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the Godbolt compiler explorer show any output for my function when compiled in release mode?

Tags:

rust

I want to use https://rust.godbolt.org to see the assembly output of this function:

fn add(a: u8, b: u8) -> u8 {
    a + b
}

Pasting this on the website works fine, but shows a lot of assembly. This is not unsurprising, given that rustc compiles my code in debug mode by default. When I compile in release mode by passing -O to the compiler, there is no output at all!

What am I doing wrong? Why does the Rust compiler remove everything in release mode?

like image 832
Lukas Kalbertodt Avatar asked Aug 08 '17 08:08

Lukas Kalbertodt


1 Answers

Godbolt compiles your Rust code as a library crate by passing --crate-type=lib to the compiler. And code from a library is only useful if it's public. So in your case, your add() function is private and is removed from the compiler completely. The solution is rather simple:

Make your function public by adding pub to it. Now the compiler won't remove the function, since it is part of the public interface of your library.

like image 141
Lukas Kalbertodt Avatar answered Nov 02 '22 12:11

Lukas Kalbertodt