使用 cargo 时如何获取带有调试信息的发布版本?

How to get a release build with debugging information when using cargo?

以下命令

$ cargo build

生成非优化构建带有调试信息。相反,

$ cargo build --release

生成优化构建没有调试信息。

有没有办法生成优化构建带有调试信息?我需要它来获得有意义的分析信息。

截至 Rust 1.57, Cargo now allows for custom profiles。这允许您定义自己的配置文件以添加调试信息:

[profile.release-with-debug]
inherits = "release"
debug = true

然后您可以在构建时使用该配置文件:

% cargo build --profile=release-with-debug
   Compiling buggin v0.1.0 (/tmp/buggin)
    Finished release-with-debug [optimized + debuginfo] target(s) in 0.48s

在此版本之前,或者如果您总是想要调试信息,您可以修改release profile以包含调试符号:

[profile.release]
debug = true

请注意 release 配置文件和 bench 配置文件不同。

另见

或者基本上是“锈蚀分析”的任何热门搜索结果:

我发现另一个不需要更改 Cargo.toml 的选项是使用 RUSTFLAGS 环境变量:

$ RUSTFLAGS=-g cargo build --release