Will #[derive(Debug)] be compiled in release build?

If it won’t. is there any way to disable #[derive(Debug)] in the release build, and make it only apply to debug/development build?

It will be compiled in release build. You may disable it, for eg.:

#[cfg_attr(debug_assertions, derive(Debug))]
struct A;

However you have to ensure, that on release build you are never using Debug trait of your struct.

3 Likes

The trait Debug is independent of whether a debug or a release build is produced. It’s just about the ability to call Debug::fmt on the type in question, which is typically meant to produce a textual representation of the type for debugging purposes.

If you want to call Debug::fmt on your type also in the release build, then you have to derive or implement Debug also in release build.

If not, you can use the usual cfg-family of attributes to conditionally restrict deriving Debug only in debug builds.

2 Likes

If you don’t ever call Debug::fmt in release builds then won’t -lto optimize it out regardless?

1 Like

Technically it should. However I see another usage of disabling Debug trait - someone may want to be ensured, that in RT he doesn’t perform any debug serialization, so this approach would make using Debug trait a hard error.

1 Like

It’s always compiled. It may be used even in release builds, e.g. by panic!().

1 Like