Can constants refer to statics?

rustc will prevent us from referring it directly, nor can addr_of!.

static FOO: u32 = 0;
const REF: &u32 = &FOO; // error
const REF2: *const u32 = std::ptr::addr_of!(FOO); // error

No practical use, just for theory.
I believe that technically rustc can do that, but practical it reject to compile I guess because static field value may changed by internal mutability. But what if we only need its address, which will never change? Thanks.

Value of constants are determined at compile time but the address of statics are determined at link time. They're not even deterministic with ASLR.


Lots of discussion here.


Thanks, I got it