How to Compile Rust Code and View Assembly Output on Compiler Explorer
Updated
Open rust.godbolt.org in a browser with JavaScript enabled, type Rust code into the left-hand source editor, and read the generated assembly in the right-hand output panel. The site compiles as you type, so no build step or local toolchain is required. This works for inspecting assembly, LLVM IR, Rust MIR/HIR, macro expansion, and optimization remarks.
Before you start
- JavaScript is required. The regular site is not functional without it; a noscript version exists but is limited.
- No login is needed to compile and view output. Save/Load and short-link sharing are available from the UI.
- The default language is Rust on this host, so the compiler dropdown already points at a Rust compiler.
Step-by-step: compile Rust and read the assembly
-
Enter your code. Click into the Source Editor on the left and type or paste a Rust function. For example:
pub fn add(a: u32, b: u32) -> u32 { a + b } -
Confirm the compiler. Use the compiler selector above the output pane to pick the Rust compiler and version you want. The output refreshes automatically after each change.
-
Read the assembly. The default output pane shows the compiled assembly for your code. Each source line is mapped to the instructions it produced, so you can trace which Rust statement generated which instructions.
-
Change the optimization level. Add flags in the compiler options field, for example
-Oor-C opt-level=3, then watch the assembly change. This is the fastest way to see how inlining and loop optimizations alter the output. -
Switch output views. Use the output dropdown to select a different representation instead of assembly:
View What it shows Assembly Final machine instructions LLVM IR Intermediate representation after Rust lowers to LLVM Rust MIR Mid-level IR, useful for borrow-check and optimization questions Rust HIR High-level IR, closer to your source Rust Macro Expansion What macros expand into Preprocessor / AST Earlier stages of the pipeline -
Add tools when needed. The "Add tool…" menu attaches extra panes such as Control Flow Graph, Stack Usage, or optimization remarks. Some tools report "The current language or compiler does not support this tool" — that message means the selected compiler cannot produce that output, so switch compilers or drop the tool.
-
Share or save. Use Share for a short or full link, or Embed in iframe to place the example in a page. Save/Load keeps named snippets.
Common snags
- Empty or stale output. Confirm the compiler finished; a compile error replaces the assembly with diagnostics.
- Assembly looks unoptimized. The default build is typically debug-style. Add
-Oor-C opt-level=3to see optimized code. - Symbols are hard to read. Enable Demangle identifiers in the output options so Rust-mangled names become readable.
- Too much noise. Use the output filters to hide directives, comments, library functions, or unused labels.
- Wrong tool for the compiler. If a pane says the tool is unsupported, pick a different Rust compiler version from the dropdown.
What you can decide from the output
Use the assembly view to check whether a function was inlined, whether bounds checks remain, or how a data structure is laid out. Use Rust MIR when the question is about borrow checking or a specific optimization pass rather than final codegen. Use LLVM IR when you want to see what the optimizer received before it produced machine code.