Should I use --cfg=debug or a Cargo feature like "internal-debug"?

I'd like to add conditional compilation to my crate to include some things specifically to local development and testing. For example deriving Debug. I have thought of two ways to do this.

  • Adding an internal only feature in the Cargo manifest. This is nice because you can use the Cargo command line flags when running tests to enable this cargo test --features=internal-debug
#[cfg_attr(feature = "internal-debug", derive(Debug))]
struct MyStruct {}
  • Adding a custom --cfg value. This is nice because its hidden from users of the crate and doesn't clutter the Cargo manifest. Its also nice because if you have multiple crates you don't need to define the feature in all of them. Downside is you have to run tests like RUSTFLAGS='--cfg=debug' cargo test
#[cfg_attr(debug, derive(Debug))]
struct MyStruct {}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.