Avatar ·

How to conditionally include attributes like `#[derive]` in Rust?

Hi everyone! I'm learning Rust and encountered a situation where I need a struct to automatically implement the `Debug` trait only in debug mode (debug build), but not in release builds. I know about `#[cfg(debug_assertions)]`, which allows conditional compilation of code blocks. But how do I apply this to attributes, especially `#[derive]`?\n\nHere's an example that doesn't work:\n\n```rust\n#[cfg(debug_assertions)]\n#[derive(Debug)]\nstruct MyStruct {\n value: i32,\n}\n```\n\nThe compiler complains that the `derive` attribute cannot be conditional. I've tried different approaches: putting `cfg` after `derive`, using nested attributes — nothing helps.\n\nI found similar discussions on English-language forums, but they are old (2016–2018). Maybe something has changed since then? Or is there a workaround?