Compiler Explorer is a website that displays the assembly generated from Rust code. It helps you see how compiler settings change the work performed at a lower level, even without knowing assembly. section 18.1.2 section 18.5
A focused comparison makes optimization easier to recognize:
- Paste a small function containing a loop that runs 1,000 times. section 18.1.2
- Inspect debug-mode output, which contains more than 100 assembly lines and extra debug information. section 18.1.2
- Change Compiler Options to -C opt-level=3 and compare the release-style output. section 18.1.2
- Notice that the optimized function becomes only three lines because the compiler can determine the loop always returns 1,000. section 18.1.2
A loop viewed through Compiler Explorer
For a function that adds 1 inside a loop from 0 to 1,000, debug compilation preserves work for runtime execution and includes diagnostic details. With optimization level 3, the compiler recognizes the fixed result and produces much shorter assembly.
