Want to add my 5 cents to this discussion.
I'm using Rust mostly for learning and developing small hobby projects at the moment so I can be wrong in my assumptions how compiler works.
Usually it goes for me like this: having some idea I'm writing bunch of structures and code and at some point want to check how it works. So I try to print them and at that moment compiler starts to tell me that I have to implement Debug for a bunch of structures. What is more annoying is that it can be either a temporary code that I'll throw away later or some internal structures that usually don't have to be printed. So compiler waste a time compiling it.
After reading this topic I've got following idea:
- Compiler should automatically generate Debug implementations for structures only when it is necessary: if user directly or indirectly (by calling function that requires Debug impl) needs it. So this should be some kind of recursive lazy Debug trait implementation.
- By default compiler doesn't generate Debug impls for crate and its dependencies at all (including std and core crates). Manually implemented Debug should stay as it is.
- Compiler should ignore existing
#[derive(Debug)]
attributes and decide itself when to implement it. - User can add
#[debug(skip)]
attribute to the structs/fields that should not be displayed. This can be used to hide some sensitive information (i.e. passwords, cryptographic data, etc.).
I see several pros in this approach:
- Developers will do less annoying coding.
- Compiler will do less work so compilation time can decrease.
- Compiler will generate smaller binaries (at least in Debug mode - in Release compiler removes unused code).
Cons:
- It can be not so easy to implement such feature in the compiler.
- It can even increase compilation time.
- Developers should not forget to add
#[debug(skip)]
for their sensitive data.
As I said I'm not very experienced in Rust compiler so all of this can be unrealistic to implement.