When I export a &str as const, why do I need to set its type and lifetime?


Say I export "Hello world!" in a library so it can be used by other crates like this:

pub const HELLO_WORLD: &'static str = "Hello world!";

Why do I need to annotate its type &str and lifetime 'static? This example says that constants “require explicit type annotation”. But why is that the case here, because &str should be infered and &str is always 'static, right? If I don’t do this the compilers throws errors.


Having a bit of type inference on “obvious” module-level constants like this seems a small but nice improvement in language ergonomy.


Last month @llogiq proposed RFC 1623 to allow these lifetimes to be omitted.


The explicit type (not the lifetime though, since it cannot be anything other than 'static) also serves as a nice reminder that by changing the type of the constant, you break API compatibility.


Thank you for all your feedback and the RFC link.