We can use llvm-dwarfdump to get the generated dwarf info:
llvm-dwarfdump -a path/to/binary | grep foo
Now we can get "foo" debuginfo in the second debug build, but not in the first release build.
It is strange, since this may be the only way to generate debuginfo in a release build (in my understanding, opt-level=3 just means release build, correct me if I am wrong).
in your Cargo.toml to enable debuginfo for the release profile. And you can use
[profile.dev]
debug = 2
opt-level = 3
to enable debuginfo and optimizations for the dev profile (which is the official name of the profile used when not passing --release)
The difference between the dev and release profiles are bigger than the used optimization level and debuginfo. According to Profiles - The Cargo Book the defaults are:
But you are free to customize existing profiles and even create new profiles. These are just defaults that work fine in many cases, but definitively not all.