While I've seen docs on using rustc
directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.
Is there a way to run Cargo that writes out assembly files?
While I've seen docs on using rustc
directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.
Is there a way to run Cargo that writes out assembly files?
You can use Cargo's
cargo rustc
command to send arguments torustc
directly:For optimized assembly:
In addition to kennytm's answer, you can also use the
RUSTFLAGS
environment variable and use the standard cargo commands:Or in release mode (with optimizations):
You can pass different values to the
--emit
parameter, including (but not limited to):mir
(Rust intermediate representation)llvm-ir
(LLVM intermediate representation)llvm-bc
(LLVM byte code)asm
(assembly)Both existing answers (using
cargo rustc
andRUSTFLAGS
) are the best ways to obtain assembly with standard tools. If you find yourself trying to look at assembly fairly often, you might want to consider using thecargo asm
subcommand. After installing it withcargo install cargo-asm
, you can print assembly like:There are a few things to pay attention to, though:
cargo asm
and it will list all symbols you can inspect.cargo build --release
before trying to look at the assembly, becausecargo asm
(apparently) only looks at the already existing build-artifacts