Currently, Rust has these three characteristics:
- The RHS of a
constcan’t refer to any
staticin any way.
- The initializer of a
staticarray of references cannot use reference-typed named
statics but can use reference-typed
- A reference-typed
constthat has an anonymous referent results in a different referent instance in each crate that the
constis used in.
This makes it hard to expose named references to statically-allocated objects such that those named references could be used in initializers of
I have trouble seeing a good reason for this considering that if the object itself is publicly named, it is legal to take its address in an initializer for a static array. (I’d like to make the exposed names be reference-typed, because they only need to be used in contexts that call for references, and it seems sad to require the user of the API to prefix non-reference-typed names with an ampersand at each point of use.)
Since it seems that #3 above is the result of
const being a (visibility-piercing) shorthand for its RHS expression rather than shorthand for the value of the RHS expression where declared, I fail to see why declaring a
const as a reference to a
static has to be prohibited.
What’s the design rationale for it being prohibited to declare a
const for a reference to a
static when the name of a
static prefixed by
& is allowed in places where similarly-typed
const with an anonymous referent would be allowed and
const seems to be a way to have a visibility-piercing shorthand for an expression? That is, why can’t a
const expand to any constant expression that itself would be legal (ignoring visibility)?