Guarantees on #[derive(Debug)]?

Are there any guarantees on the output of #[derive(Debug)] ?

  1. Fix a compiler version. Is it guaranteed that the output of #[derive(Debug)] is the identical ?

  2. Across compiler versions, is it guaranteed that the output of #[derive(Debug)] changes rarely, if at all ?

Well, the output of #[derive(Debug)] has got little to do with the compiler per-say. It is a proc-macro which on expansion call debug() on all the fields/variants. Only some of the root level "things", like u32 have a direct impl. Almost everything else is a derive. So, if:

  • Some struct changes its Debug impl
  • Or the proc-macro is itself changed
    the output will change, otherwise not. Now I imagine that stdlib maintainers won't randomly go about changing it. But I don't think there are guarantees.

No.

7 Likes

Yes, it is deterministic.

No. In practice, it shouldn't change much, since for many types there's one main way to make a human-readable debugging output, but this isn't guaranteed.

1 Like

If you want something with a guarantee, I suggest using #[derive(Serialize)] from serde instead.

Then you can serialize to something like json or ron if you want programmer-readable versions of your data objects.

3 Likes

This is my fault for not being clear. The structs do not change. My only concern is the macro changing.