How to Use cargo-flamegraph for Performance Profiling

Install cargo-flamegraph and run it with your binary to visualize performance bottlenecks in a flamegraph.

cargo-flamegraph is not a standard Cargo command; you must install the cargo-flamegraph crate or use perf with flamegraph tools to profile Rust binaries. Install the tool and run your binary with profiling enabled to generate a flamegraph.

cargo install cargo-flamegraph
cargo flamegraph --release -- ./your-binary-args

Alternatively, use perf directly if you prefer manual control over sampling:

perf record -g -- cargo run --release
perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg