Debug is not always implemented. Therefore, when we implement generic functions of types, we have have to choose either: Add
where T:Debug, or give up
expect or other utilities.
Is that true? Is there any way to use debug string only when
Debug is implemented and use another default string when is not implemented?
This comes up periodically; the usual suggestion is to use the (unstable, nightly-only) specialization feature, as in these posts:
Something like this seems like a very reasonably application of the unstable
specialization feature. Since you’re only caring about debugging anyways, perhaps you’re fine with using a nightly compiler for the debugging. In this case, you can do something like this: Rust Playground
You can use specialization to reach Debug impls, even if you don’t have the bound. I’m going to use this for debugging a lot.
Playground example here
That’s the brave new world of non-parametricity.
The second one worked, with scary warning.
It would be useful for temporal
Yes, the scary warning (applicable to both examples since they use
feature(specialization), one of them just silences it) - it indicates that there needs to be found a way to do the same thing using
feature(min_specialization) which is likely closer to how specialization will be allowed to be used in future Rust.
Note that “the same thing”
must not be “the same thing”. The proposed workarounds, e.g. the literally macro I made in the other thread, or your wrapper,
really aren’t supposed to be used in production, because they’re (both) inherently unsound.
@Vill.Snow, that “scary warning” you currently get isn’t without reason.
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.