Associated const pointer equality

I have a trait with an associated const of a &'static reference type, along with a blanket impl:

trait HasMetadata {
    const META: &'static Metadata;

impl<T> HasMetadata for T {
    const META: &'static Metadata = &Metadata {
        /* ... */

I expected to be able to use ptr::eq to compare the values, but it seems the same T can return different pointer values for the associated const. Specifically, in my case, they seemed to match in my regular tests, but not in my doctests, despite the code being identical. This is not a major problem, but I am curious if someone can point me in the direction of documentation that explains the nuance of this behavior.

Constant items - The Rust Reference

Constants may refer to the address of other constants, in which case the address will have elided lifetimes where applicable, otherwise – in most cases – defaulting to the static lifetime. (See static lifetime elision.) The compiler is, however, still at liberty to translate the constant many times, so the address referred to may not be stable.


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.